简体   繁体   English

Next.js:getServerSideProps 无法使用 Next.js 和 Next-redux-wrapper 以及 TypeScript

[英]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-thunkgetServerSideProps文档中发送操作时,我不断收到以下 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.

相关问题 Next.js:使用带有 TypeScript 的 next-redux-wrapper 在 getServerSideProps 中调用 Thunks? - Next.js: Calling Thunks in getServerSideProps with next-redux-wrapper with TypeScript? Next.js GetServerSideProps 自定义包装器 TypeScript - Next.js GetServerSideProps custom wrapper TypeScript 如何正确使用带有“Next.js”、“Redux-ToolKit”和 Typescript 的“next-redux-wrapper”? - How to use "next-redux-wrapper" with "Next.js", "Redux-ToolKit" and Typescript properly? 如何使用 typescript、next-redux-wrapper、getServerSideProps? - how to use typescript, next-redux-wrapper, getServerSideProps? nextjs 中 getServerSideProps 中的 next-redux-wrapper - next-redux-wrapper in getServerSideProps in nextjs Next.JS typescript 通过 getServerSideProps 将 props 返回给外部组件 - Next.JS typescript pass getServerSideProps returned props to external component getServerSideProps 上的 Supbase 策略 - Next.js - Supabase policies on getServerSideProps - Next.js Next.js getServerSideProps 始终未定义 - Next.js getServerSideProps is always undefined Redux 工具包 typescript 类型问题与 next js 和 next-redux-wrapper - Redux toolkit typescript type issue with next js and next-redux-wrapper 在 Next.js 和 TypeScript 中使用 getInitialProps - Using getInitialProps in Next.js with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM