Input + Output = True
redux persist ์ฌ์ฉ๋ฒ (๋ฆฌ๋์ค ์๋ก๊ณ ์นจ ๋ฐ์ดํฐ ์ ์ง) ๋ณธ๋ฌธ
redux persist ์ฌ์ฉ๋ฒ (๋ฆฌ๋์ค ์๋ก๊ณ ์นจ ๋ฐ์ดํฐ ์ ์ง)
IOTrue 2023. 1. 5. 00:17๐ redux-persist ์ฌ์ฉ๋ฒ
โ redux persist๋ฅผ ์ฌ์ฉํ๋ ์ด์ ?
๋ฆฌ๋์ค์ store๋ ํ์ด์ง ์๋ก๊ณ ์นจ์ด ๋ ๋ state ๊ฐ์ด ์ด๊ธฐํ ๋๋ค.
๋ง์ฝ ํ์ด์ง ์๋ก๊ณ ์นจ์ ํด๋ state ๊ฐ์ ์ ์งํ๊ณ ์ถ๋ค๋ฉด
์ ์ฅํ๊ณ ์ ํ๋ reducer state๋ฅผ LocalStorage ๋๋ SessionStorage์ ์ ์ฅํด์
์๋ก๊ณ ์นจ์ ํด๋ ์ ์ฅ๊ณต๊ฐ์ ์๋ ๋ฐ์ดํฐ๋ฅผ redux์ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
์ด ๋ฐฉ๋ฒ์ redux-persist๋ฅผ ํตํด ์ฌ์ฉํ ์ ์๋ค.
โ redux persist ์ธํ ๋ฐฉ๋ฒ
redux๊ฐ ์ด๋ฏธ ์ธํ ๋์ด ์๋ ์ํฉ์ ๊ฐ์ ํ๊ณ ์งํ
redux persist ์ค์น
yarn add redux-persist
reducer์ persist store ์ ์
//configStore.js
import { createStore, combineReducers } from "redux";
โ import { persistReducer } from "redux-persist";
โ import storage from "redux-persist/lib/storage";
//localStorage ์ฌ์ฉ : import storage from 'redux-persist/lib/storage
//sessionStorage ์ฌ์ฉ : import storageSession from 'redux-persist/lib/storage/session
import todos from '../modules/todo';
โ const persistConfig = {
key: "root", //root : ์ต์๋จ์ ํ์ผ๋ถํฐ ์ ์ฉ
storage, //LocalStorage ์ ์ฅ, sessoin -> storage : storageSession
whitelist: ["todos"] //์ ์ฅํ reducer
//blacklist: ["์ด๊ฒ๋ง ์ ์ธ"]
};
const rootReducer = combineReducers({ todos });
โ const perReducer = persistReducer(persistConfig, rootReducer);
const store = createStore(perReducer);
export default store;
persist๊ฐ ์ ์ฉ๋ store ์ฌ์ฉ
// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import store from "./redux/config/configStore";
import { Provider } from "react-redux";
โ import { persistStore } from "redux-persist";
โ import { PersistGate } from "redux-persist/integration/react";
โ const persistor = persistStore(store);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
โ <PersistGate loading={null} persistor={persistor}>
<App />
<PersistGate/>
</Provider>
);
๐ก LocalStorage , SessionStorage
์๋ฒ์ ์ํฅ์ ๋ฐ์ง ์๋ ๋ธ๋ผ์ฐ์ ์ ๋ด๋ถ ์ ์ฅ์
LocalStorage : ์๋ก๊ณ ์นจ์ ํด๋ ๋ฐ์ดํฐ๊ฐ ์ด๊ธฐํ๋์ง ์์
SessionStorage : ์๋ก๊ณ ์นจ ์ ๋ฐ์ดํฐ ์ด๊ธฐํ๋จ
์ฟ ํค, ์ธ์ , ์บ์, ์คํ ๋ฆฌ์ง
๐ ์ฟ ํค, ์ธ์ , ์บ์, ์คํ ๋ฆฌ์ง ์ฟ ํค์ ์ธ์ ์ ์ฌ์ฉํ๋ ์ด์ ์ ์ฐจ์ด์ ์์ฝ HTTP์ connectionless, stateless ํ๋กํ ์ฝ ๋ก ์ธํด ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์ ํต์ ์ ํ ๋๋ง๋ค ๊ณ์ ์ธ์ฆ์ด ํ์ํ๊ธฐ ๋๋ฌธ์ ์ฌ
iotrue.tistory.com
์ฐธ๊ณ
https://github.com/rt2zz/redux-persist
https://kyounghwan01.github.io/blog/React/redux/redux-persist/
'๐จFrontEnd > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๊ธฐ ๋น๋๊ธฐ / ์๋ฒ ํต์ (feat. react axios) (0) | 2023.01.15 |
---|---|
๋ฆฌ์กํธ ๋ผ์ดํ์ฌ์ดํด(ํด๋์คํ vs ํจ์ํ), React hooks (0) | 2023.01.08 |
React - redux ์ดํดํ๊ธฐ (๋ฆฌ๋์ค ๊ฐ๋ ์ ๋ฆฌ) (1) | 2023.01.04 |
React - State, Props, ๋ฆฌ๋ ๋๋ง ๋ฐ์ ์กฐ๊ฑด (0) | 2023.01.02 |
๋ฆฌ์กํธ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ํด์ (feat. useEffect) (0) | 2022.12.28 |