React-无法发送带有额外参数的redux-thunk操作

我尝试使用redux-thunk和redux的打字稿。我想将对象传递给任何动作(我正在使用依赖注入,并且需要传递对象服务或字符串)

我得到这个错误;

  Overload 1 of 2, '(action: AnyAction): AnyAction', gave the following error.
    Argument of type '(dispatch: any, getState: AppState, extraArg: string) => Promise<any>' is not assignable to parameter of type 'AnyAction'.

如下所示:

store.ts:

const middlewares = [
    routerMiddleware(history),
    thunkMiddleware.withExtraArgument('bar') as ThunkMiddleware<
      AppState,
      Actions,
      string
    >
  ];
  const middleWareEnhancer = applyMiddleware(...middlewares);

  const store = createStore(
    rootReducer(history),
    preloadState,
    composeWithDevTools(middleWareEnhancer)
  );

我尝试发送操作的index.ts是:

(store.dispatch as ThunkDispatch<{}, void, AnyAction>)(getAll());

我的动作文件正常工作:

export const getall = () => async (dispatch: any) => {
  try {
    dispatch({ type: GETALL_REQUEST });
    return dispatch({ type: GETALL_SUCCESS, payload: null });
  } catch (e) {
    return dispatch({ type: GETALL_FAILURE, error: e });
  }
};

我的动作(放入其中的一部分)引发了错误

export const getAll = () => async (
  dispatch: any,
  getState: AppState,
  extraArg: string
) => {....action logic}

然后我得到了错误:

(store.dispatch as ThunkDispatch<{}, void, AnyAction>)(getAll());
费奥多尔

纠正错误的最简单方法:

(store.dispatch as ThunkDispatch<AppState, string, AnyAction>)(getAll());

export const getAll = () => async (
  dispatch: any,
  getState: () => AppState,
  extraArg: string
) => {....action logic}
  1. ThunkDispatch接受以下类型参数:S-状态,E-扩展arg,A-动作。因此,您对的使用ThunkDispatch应与状态和扩展arg的类型匹配。

  2. Thunk action是一个接受以下参数dispatch的函数是一个函数,getState也是一个返回state和的函数extraArg因此,请确保这getState是一个功能。

为了避免dispatch强制转换,我建议在创建时正确键入商店。当您使用applyMiddleware哪个接受数组时,应将type参数传递给它。它接受将在应用特拉华州之后使用的调度类型。

const middlewares = [thunk.withExtraArgument("bar")];
const middleWareEnhancer = applyMiddleware<
  ThunkDispatch<AppState, string, Actions>
>(...middlewares);

结果,您将获得具有正确类型的调度功能的商店。而且您无需铸造就可以使用它。

store.dispatch(getAll());

工作演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何测试分派 Redux / Thunk 操作的 React 组件

React / TypeScript / Redux / Thunk操作未调度,状态未更新

React Redux Thunk一次调用即可触发多项操作

如何使用thunk和useDispatch(react-redux挂钩)从操作中返回承诺?

Redux Thunk 操作未执行

Redux Thunk和异步操作

带有 thunk 的 Jest 测试 redux 操作不包括 statemets

Redux Thunk操作,axios返回多个值

从React Redux Thunk返回承诺

无法读取Redux thunk操作上未定义的属性'then'

React / Redux-使用Thunk将JSON数据从一个API调用传递到另一个Redux操作

分派操作后Redux Thunk回调吗?

redux-thunk调度方法触发未定义的操作

如何使用Redux Thunk链接动态系列的异步操作?

使用Redux-Thunk / Axios从onUploadProgress事件调度操作

如何通过redux-thunk中的fetching操作访问状态?

如何使用 Jest Mock 模块测试 Redux Thunk 异步操作

如何在带有thunk的react-redux挂钩中进行异步调用?

派遣Redux Thunk重新渲染React组件

带有 Redux 的 React JS 不会更新触发操作的状态

cb &&'function'=== typeof cb && cb(data)在带有redux-thunk的Action(React-redux)中如何工作?

如何使用Redux 4.0.1,Redux-Thunk 2.3.0和Typescript 3.1.3正确键入Redux-thunk操作和mapDispatchToProps

发送可重用的React组件的Redux操作

redux thunk是否会等到所有较早分派的操作都修改了商店之后?

redux,react-redux,redux-thunk有什么区别?

如何在React上正确使用redux-thunk?

使用 react native 和 redux 进行异步调用,thunk

Redux Thunk vs在React组件中进行api调用

react-redux 中的 API 调用与 thunk 不起作用