리액트 상태는 새로고침하면 전부 날라간다. 이를 유지하기 위해서는 localstorage 또는 sessionstorage를 사용해야 되는데 redux 환경에선 redux-persist라는 라이브러리가 이를 조작할 수 있다.
참고URL: kyounghwan01.github.io
import storage from 'redux-persist/lib/storage
import storageSession from 'redux-persist/lib/storage/session
// reducers/index.js
import { combineReducers } from "redux";
➊ import { persistReducer } from "redux-persist";
➋ import storage from "redux-persist/lib/storage";
➌ const persistConfig = {
key: "root",
// localStorage에 저장합니다.
storage,
// auth, board, studio 3개의 reducer 중에 auth reducer만 localstorage에 저장합니다.
whitelist: ["counter"]
// blacklist -> 그것만 제외합니다
};
const rootReducer = combineReducers({
auth,
board,
studio
});
➍ export default persistReducer(persistConfig, rootReducer);
// 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";
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"));