[英]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-thunk
时next-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.