我一直在尝试使用react redux项目实现redux-persist。这就是我所拥有的。
store.js:
import {compose, createStore, applyMiddleware} from 'redux';
import {persistStore, autoRehydrate} from 'redux-persist';
import {combineReducers} from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducers/reducer';
import modalReducer from './reducers/modal-reducer';
const store = createStore(
combineReducers({
reducer,
modalReducer,
}),
compose(
applyMiddleware(thunk),
autoRehydrate()
),
)
export default persistStore(store);
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {BrowserRouter} from 'react-router-dom';
import './index.css';
import App from './App';
import store from './store';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>,
document.getElementById('root'));
registerServiceWorker();
我知道如果我要在store.js中导出默认的just store而不是persistStore(store),那么代码可以工作,我不会遇到错误。有什么想法吗?
答案 0 :(得分:0)
我相信发生这种情况的原因是因为module.exports存储了对象。当你调用export default persistStore(store);
时,你试图导出调用,而不是对象。这就是为什么export default store;
有效的原因,我相信nathan hayfield所写的也会起作用。