当我尝试为 redux 进行配置时,我开始将 nextjs 集成到我的 react 应用程序中,但我发现它需要对 store 进行额外配置,例如 get preloadState 和其他东西不喜欢不理解就复制过去 方便我编辑和维护
function initStore(preloadedState = initialState) {
return createStore(
reducer,
preloadedState,
composeWithDevTools(applyMiddleware())
)
}
export const initializeStore = (preloadedState) => {
let _store = store ?? initStore(preloadedState)
if (preloadedState && store) {
_store = initStore({
...store.getState(),
...preloadedState,
})
store = undefined
}
if (typeof window === 'undefined') return _store
if (!store) store = _store
return _store
}
export function useStore(initialState) {
const store = useMemo(() => initializeStore(initialState), [initialState])
return store
}