如何连接中间件和redux-persis



如何连接中间件和redux-persis?下面是我的index.js和store.js,我不知道如何正确地连接它们。现在我有一个错误"Uncaught TypeError:无法读取未定义的属性"dispatch"我该怎么修?非常感谢。

存储

const persistConfig ={
key: 'root',
storage
};
const persistedReducer = persistReducer(persistConfig, Reducer);
const persistor = persistStore(store);

const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
}) : compose;

const store = createStore(
persistedReducer,
composeEnhancers(
applyMiddleware(thunk)
)
)
export {store, persistor};

index.js

import {BrowserRouter} from "react-router-dom";
import {Provider} from "react-redux";
import {store, persistor} from "./Store/store.js";
import {PersistGate} from "redux-persist/integration/react";
const app =(
<Provider store={store} >
<PersistGate loading={null} persistor={persistor}>
<BrowserRouter>
<App />
</BrowserRouter>
</PersistGate>
</Provider>
)
ReactDOM.render(app, document.getElementById("root"));

看起来你正在尝试在创建存储之前将其持久化。我建议传入预加载状态:

const persistConfig ={
key: 'root',
storage
};
const persistedReducer = persistReducer(persistConfig, Reducer);
const preloadedState = {
// Fill out accordingly
}
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
}) : compose;

const store = createStore(
persistedReducer,
preloadedState,
composeEnhancers(
applyMiddleware(thunk)
)
)
const persistor = persistStore(store);
export {store, persistor};

最新更新