[英]Next.js: getServerSideProps not working using Next.js with Next-redux-wrapper with TypeScript
When I'm trying to dispatch action as in documentation from the getServerSideProps
using next-redux-wrapper
store and redux-thunk
i keep getting the following typescript error:当我尝试使用next-redux-wrapper
store 和redux-thunk
从getServerSideProps
的文档中发送操作时,我不断收到以下 typescript 错误:
ts(2322): Type '({ req }: GetServerSidePropsContext<ParsedUrlQuery>) => Promise<void>' is not assignable to type 'GetServerSideProps<any, ParsedUrlQuery>'.
Type 'Promise<void>' is not assignable to type 'Promise<GetServerSidePropsResult<any>>'.
Type 'void' is not assignable to type 'GetServerSidePropsResult<any>'.
My code is as follows:我的代码如下:
// category.tsx
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ req }) => {
await store.dispatch(fetchCategory())
}
)
// categoriesAction.ts
export const fetchCategory = () => {
return async (dispatch: Dispatch<CategoriesAction>) => {
try {
const res = await axios.get("/category")
dispatch({
type: CategoriesActionTypes.LOAD_CATEGORY_SUCCESS,
payload: res.data,
})
} catch (err) {
dispatch({
type: CategoriesActionTypes.LOAD_CATEGORY_ERROR,
payload: err.code,
})
}
}
}
// store.ts
const makeStore = (context: Context) =>
createStore(reducer, composeWithDevTools(applyMiddleware(thunk)))
export const wrapper = createWrapper<Store<RootState>>(makeStore, {
debug: true,
})
// reducers.ts
const rootReducer = combineReducers({
categories: categoriesReudcer,
})
export const reducer = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state, // use previous state
...action.payload, // apply delta from hydration
}
if (state.count) nextState.count = state.count // preserve count value on client side navigation
return nextState
} else {
return rootReducer(state, action)
}
}
export type RootState = ReturnType<typeof rootReducer>
Your code你的代码
// category.tsx
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ req }) => {
await store.dispatch(fetchCategory())
}
)
Try this尝试这个
// category.tsx
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ req }) => {
try {
await store.dispatch(fetchCategory())
return { props: {} }
} catch (e) {
return { props: {} }
}
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.