머무르고 있는 페이지에서, useSelector로 store 상태만 가져와 사용하여 개발하던 도중, 새로 고침하면 기존의 store가 초기화되는 문제를 겪었습니다.
해결 방법으로 LocalStorage와 SessionStorage의 storage를 redux에서 사용하게 해주는 Redux-Persist 라이브러리를 사용하여 해결했습니다.
import { combineReducers } from "redux";
import cart from "./cart";
import loading from "./loading";
// Redcer-Persist
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
const persistConfig = {
key: "root",
storage: storage,
};
const rootReducer = combineReducers({
data,
cart,
loading,
});
export function* rootSaga() {
yield all([dataSaga()]);
}
export default persistReducer(persistConfig, rootReducer);
import { createStore, applyMiddleware } from "redux";
import { createLogger } from "redux-logger";
import rootReducer, { rootSaga } from "./index";
import createSagaMiddleware from "redux-saga";
import { composeWithDevTools } from "redux-devtools-extension";
// Redux-Persist
import { persistStore } from "redux-persist";
const logger = createLogger();
const sagaMiddleware = createSagaMiddleware();
export const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(sagaMiddleware))
);
sagaMiddleware.run(rootSaga);
export const persistor = persistStore(store);
export default { store, persistor };
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
// Redux-Persist
import { store, persistor } from "./reducer/store";
import { PersistGate } from "redux-persist/integration/react";
ReactDOM.render(
<Provider store={store}>
<PersistGate persistor={persistor}>
<BrowserRouter>
<App />
</BrowserRouter>
</PersistGate>
</Provider>,
document.getElementById("root")
);