【问题标题】:Argument of type 'AsyncThunkAction<any, number | undefined, {}>' is not assignable to parameter of type 'AnyAction''AsyncThunkAction<any, number | 类型的参数未定义,{}>' 不可分配给“AnyAction”类型的参数
【发布时间】:2021-11-18 17:55:43
【问题描述】:

我目前正在开发基于 Next.js 的 Web 应用程序。

我们正在使用 Next.js + Redux + Redux-Thunk。我问这个问题是因为开发过程中发生了错误。

'AsyncThunkAction' 是 不可分配给“AnyAction”类型的参数。属性“类型”是 'AsyncThunkAction' 但是 在“AnyAction”类型中是必需的。

在组件内,dispatch 通常接收 thunk 函数作为参数,但 store.dispatch() 不能接收 thunk 函数作为 getServerSideProps 函数的参数。

export const getServerSideProps = wrapper.getServerSideProps((store) => async () => {
    store.dispatch(getPeoples(1));
    return {
      props: {
        peoples: data,
      },
    };
  }
);

这是我的store.tsx

const makeStore = () => configureStore({
    reducer: rootReducer,
    middleware: getDefaultMiddleware => getDefaultMiddleware(),
    // devTools,
    // preloadedState,
    // enhancers:
});

export type AppStore = ReturnType<typeof makeStore>;
export type AppState = ReturnType<AppStore['getState']>;
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, AppState, unknown, 
Action>;
export type AppThunkDispatch = ThunkDispatch<{}, void, AnyAction>

export default createWrapper(makeStore);

有好的解决办法吗?

【问题讨论】:

    标签: reactjs typescript redux next.js redux-thunk


    【解决方案1】:

    createWrapper(从 v7 开始)应该能正确识别它。直接导入store时能不能测试一下有没有同样的问题? 当您的 node_modules 某处都安装了 redux 4.0.5 和 4.1.1 时,存在一个已知问题,dispatch 类型消失了,您可以通过这种方式检测到 - 如果您使用 store 遇到同样的问题直接,您需要修复该安装问题。

    【讨论】:

    • 谢谢。我会按照您的建议进行检查。
    猜你喜欢
    • 2022-01-05
    • 2021-10-19
    • 2019-09-07
    • 2021-01-03
    • 2020-06-19
    • 2019-10-23
    • 2020-11-13
    • 2020-05-19
    • 2020-06-19
    相关资源
    最近更新 更多