adi*_*iti 5 typescript reactjs redux redux-thunk next.js
我正在尝试使用storethunk从getServerSidePropsNext.js 中发送一个next-redux-wrapper。但是,我不断收到以下打字稿错误:
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'.
Run Code Online (Sandbox Code Playgroud)
我之前没有在 Redux 中使用过 TypeScript,所以不确定我在这里做错了什么......
我的代码如下:
// page.tsx
export const getServerSideProps = wrapper.getServerSideProps(
async ({ store, params }) => {
store.dispatch(myThunkFunction(params.id));
return {
props: {
id: params.id,
},
};
});
Run Code Online (Sandbox Code Playgroud)
// thunks.ts
export const myThunkFunction = id => {
return async dispatch => {
...
};
};
Run Code Online (Sandbox Code Playgroud)
这似乎是next-redux-wrapperusing 时的一个已知问题redux-thunk,其中indispatch返回的类型(或者我正在使用的类型)不是.next-redux-wrappergetServerSidePropsgetInitialPropsThunkDispatch
我花了很多时间试图找出问题所在,并设法通过创建应用程序 thunk 调度类型来找到解决方法,例如
export type AppThunkDispatch = ThunkDispatch<ApplicationState, void, AnyAction>
当在getServerSideProps(或getInitialProps) 中使用它时,您只需将store.dispatch它投射到它。
const thunkDispatch = store.dispatch as AppThunkDispatch
我还没有尝试过,getServerSideProps但我假设传递给它的商店将与传递给它的商店相同getInitialProps......
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |