首页 > 解决方案 > 使用 typescript 将类型定义为 history.push() 已作为有效负载传递给 saga?

问题描述

{ useHistory } from react-router-dom在反应组件中使用过。并在按钮单击时尝试通过 action.ts 将历史实例作为有效负载传递给 saga

组件.tsx

import React from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import {recievingHistoryAsPayload } from 'store/actions';


const Component: React.FC = () => {
  const history = useHistory();
  const dispatch = useDispatch();

  const sendHistoryObj = () => {
   dispatch(recievingHistoryAsPayload({ payload: history }));
 };
 return (
  <Button
      onClick={sendHistoryObj}
  />
 );
};

export default Component;

动作.ts

export const recievingHistoryAsPayload = createAction(
 Types.RECEIVING_HISTORY_AS_PAYLOAD,
 (data) => data
);

saga.ts

export function* recievingHistoryAsPayloadSaga(action: any): Generator {
  yield call(action?.payload?.push, '/');
}

在 saga.ts 中,对于动作有效负载 eslint 正在引发与消息的交战Argument 'action' should be typed with a non-any type.

应该为 saga.ts 中的操作定义什么类型

标签: javascriptreactjstypescript

解决方案


推荐阅读