首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next.js:使用TypeScript的next-redux-wrapper在getServerSideProps中调用Thunks?

Next.js:使用TypeScript的next-redux-wrapper在getServerSideProps中调用Thunks?
EN

Stack Overflow用户
提问于 2020-06-12 04:10:43
回答 1查看 1.7K关注 0票数 5

我正在尝试使用next-redux-wrapper存储从Next.js中的getServerSideProps分派一个thunk。但是,我一直收到以下typescript错误:

代码语言:javascript
复制
TS2345: Argument of type '(dispatch: any) => Promise<any>' is not assignable to parameter of type 'AnyAction'.   Property 'type' is missing in type '(dispatch: any) => Promise<any>' but required in type 'AnyAction'.

我以前没有在Redux中使用过TypeScript,所以不确定我在这里做错了什么……

我的代码如下:

代码语言:javascript
复制
// page.tsx

export const getServerSideProps = wrapper.getServerSideProps(
  async ({ store, params }) => {
    store.dispatch(myThunkFunction(params.id));

    return {
      props: {
        id: params.id,
      },
    };
  });
代码语言:javascript
复制
// thunks.ts

export const myThunkFunction = id => {
  return async dispatch => {
    ...
  };
};
EN

回答 1

Stack Overflow用户

发布于 2020-10-02 07:53:27

It seems to be a known issue with next-redux-wrapper when using redux-thunk,其中next-redux-wrappergetServerSideProps中返回的dispatch类型(或者我正在使用的getInitialProps )不是ThunkDispatch

我花了很多时间试图找出问题所在,并设法通过创建一个应用程序thunk分派类型找到了解决方法,例如

export type AppThunkDispatch = ThunkDispatch<ApplicationState, void, AnyAction>

getServerSideProps (或getInitialProps)中使用它时,只需将store.dispatch转换为它即可。

const thunkDispatch = store.dispatch as AppThunkDispatch

我还没有在getServerSideProps上尝试过,但我假设传递给它的商店将与传递给getInitialProps的商店相同……

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62332738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档