[英]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.