yarn add redux-persist //설치
reducer에 persist store 정의
// reducers/index.js
import { combineReducers } from "redux";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import auth from "./auth";
import board from "./board";
import studio from "./studio";
const persistConfig = {
key: "root",
// localStorage에 저장
storage,
// auth, board, studio 3개의 reducer 중에 auth reducer만 localstorage에 저장
whitelist: ["auth"]
// blacklist -> 그것만 제외
};
export const rootReducer = combineReducers({
auth,
board,
studio
});
export default persistReducer(persistConfig, rootReducer);
persist store 사용
// src/index.js
import React from "react";
import ReactDOM from "react-dom";
import { createStore, applyMiddleware, compose } from "redux";
import { Provider } from "react-redux";
import { persistStore } from "redux-persist";
import { PersistGate } from "redux-persist/integration/react";
import App from "./App";
import configureStore from "./store";
import { rootReducer } from "./reducers";
const store = createStore(rootReducer);
const persistor = persistStore(store);
const Root = () => (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
);
ReactDOM.render(<Root />, document.getElementById("root"));