繁体   English   中英

如何在 redux 工具包(带打字稿)中设置 preloadedState?

[英]How to set the preloadedState in redux toolkit (with typescript)?

目前我正在这样做

export let store = null;

export default function getStore(incomingPreloadState?: any) {
  store = configureStore({
    reducer: { 
      content: ContentSlice
    },
    preloadedState: incomingPreloadState,
  });
  return store;
}
export type AppState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

显然,我收到了商店可能是 null 的错误

这里还有很多事情要做。 您可能不希望模块全局store变量始终是“最后创建的存储” - 至少我不建议这样做,因为您最终可能会以这种方式混合多个存储实例。

要为商店获取正确的类型,您必须手动调用combineReducers

const rootReducer = combineReducers({
  content: ContentSlice
})

export default function getStore(incomingPreloadState?: AppState) {
  const store = configureStore({
    reducer: rootReducer,
    preloadedState: incomingPreloadState,
  });
  return store;
}
export type AppState = ReturnType<typeof rootReducer>
export type AppDispatch = ReturnType<typeof getStore>['dispatch']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM