简体   繁体   English

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

[英]Next.js: Calling Thunks in getServerSideProps with next-redux-wrapper with TypeScript?

I'm trying to dispatch a thunk from the getServerSideProps in Next.js using next-redux-wrapper store.我正在尝试使用next-redux-wrapper存储从 Next.js 中的getServerSideProps发送一个thunk However, I keep getting the following typescript error:但是,我不断收到以下 typescript 错误:

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'.

I haven't used TypeScript with Redux before, so not sure what I'm doing wrong here...我以前没有使用过 TypeScript 和 Redux,所以不知道我在这里做错了什么......

My code is as follows:我的代码如下:

// page.tsx

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

    return {
      props: {
        id: params.id,
      },
    };
  });
// thunks.ts

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

It seems to be a known issue with next-redux-wrapper when using redux-thunk , where the dispatch type returned by next-redux-wrapper in getServerSideProps (or getInitialProps as that is what I'm using) isn't a ThunkDispatch . 使用redux-thunknext-redux-wrapper似乎是一个已知问题,其中getServerSideProps (或我正在使用的getInitialProps )中的next-redux-wrapper返回的dispatch类型不是ThunkDispatch

I spent a lot of time trying to figure out what the issue was and managed to find a work around by creating an application thunk dispatch type like我花了很多时间试图找出问题所在,并通过创建一个应用程序 thunk 调度类型来设法找到解决方法

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

And when using it inside getServerSideProps (or getInitialProps ) you would just cast the store.dispatch to it.当在getServerSideProps (或getInitialProps )中使用它时,您只需将store.dispatch为它。

const thunkDispatch = store.dispatch as AppThunkDispatch

I haven't tried it with getServerSideProps but I'm assuming the store passed to it will be the same as the store passed to getInitialProps ...我没有用getServerSideProps尝试过,但我假设传递给它的商店将与传递给getInitialProps的商店相同......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Next.js GetServerSideProps 自定义包装器 TypeScript - Next.js GetServerSideProps custom wrapper TypeScript 将 Next.js 与 next-redux-wrapper 一起使用时,getInitialProps 似乎不起作用 - getInitialProps doesn't seem to work when using Next.js with next-redux-wrapper 使用链接导航时,具有 next-redux-wrapper 状态的 Next.js 被重置为初始值 - Next.js with next-redux-wrapper state is being reset to initial value when navigating using Link Redux 工具包 typescript 类型问题与 next js 和 next-redux-wrapper - Redux toolkit typescript type issue with next js and next-redux-wrapper 无法使用 next-redux-wrapper 使 Redux (Thunks) 与 NextJS 一起工作:未调用 getInitalProps - Can't get Redux (Thunks) to work with NextJS using next-redux-wrapper: getInitalProps not called Next.js 如何获取ServerSideProps - Next.js how to getServerSideProps Next.Js Redux Wrapper w/Typescript - 实现错误,存储不存在为类型 - Next.Js Redux Wrapper w/ Typescript - Implementation error, store does not exist as type 使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序 - Add Google Analytics to Nextjs app with next-redux-wrapper next-redux-wrapper HYDRATE 被多次调用 - next-redux-wrapper HYDRATE getting called multiple times 如何使用 next-redux-wrapper 在 nextjs 页面之间共享 state? - How to share state between nextjs pages with next-redux-wrapper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM