使っredux-persist
ていますが最高です。
しかし、開発だけで使おうとするとうまくいかないようです。ここで何が間違っていますか?
store.js
import thunk from 'redux-thunk';
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import rootReducer from './reducers';
const persistConfig = {
key: 'root',
storage,
};
let store;
let persistor;
// Show the Redux Devtools only in development mode.
if (process.env.NODE_ENV === 'development') {
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const persistedReducer = persistReducer(persistConfig, rootReducer);
store = createStore(persistedReducer, composeEnhancers(applyMiddleware(thunk)));
persistor = persistStore(store);
} else {
store = createStore(rootReducer, applyMiddleware(thunk));
}
export { store, persistor };
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import App from './App';
import { store, persistor } from './store';
import registerServiceWorker from './registerServiceWorker';
if (process.env.NODE_ENV === 'development') {
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>,
document.getElementById('root'),
);
} else {
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'),
);
}
registerServiceWorker();