[英]Next.js GetServerSideProps custom wrapper TypeScript
[英]Next.js: Calling Thunks in getServerSideProps with next-redux-wrapper with TypeScript?
我正在尝试使用next-redux-wrapper
存储从 Next.js 中的getServerSideProps
发送一个thunk
。 但是,我不断收到以下 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'.
我以前没有使用过 TypeScript 和 Redux,所以不知道我在这里做错了什么......
我的代码如下:
// 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 => {
...
};
};
使用redux-thunk
时next-redux-wrapper
似乎是一个已知问题,其中getServerSideProps
(或我正在使用的getInitialProps
)中的next-redux-wrapper
返回的dispatch
类型不是ThunkDispatch
。
我花了很多时间试图找出问题所在,并通过创建一个应用程序 thunk 调度类型来设法找到解决方法
export type AppThunkDispatch = ThunkDispatch<ApplicationState, void, AnyAction>
当在getServerSideProps
(或getInitialProps
)中使用它时,您只需将store.dispatch
为它。
const thunkDispatch = store.dispatch as AppThunkDispatch
我没有用getServerSideProps
尝试过,但我假设传递给它的商店将与传递给getInitialProps
的商店相同......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.