简体   繁体   English

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

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

At the moment I'm doing this目前我正在这样做

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

Obviously, I'm getting the error that the store could possibly be null显然,我收到了商店可能是 null 的错误

There's a lot more going on here.这里还有很多事情要做。 You probably don't want that module-global store variable that would always be "the last store created" - at least I wouldn't recommend that, as you could end up with mixing up multiple store instances that way.您可能不希望模块全局store变量始终是“最后创建的存储” - 至少我不建议这样做,因为您最终可能会以这种方式混合多个存储实例。

To get the right type for the store, you'll have to call combineReducers manually.要为商店获取正确的类型,您必须手动调用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