kohigowild
redux-persist ์ ์ฉ (feat. react, redux-toolkit) ๋ณธ๋ฌธ
๐คฏ ์ ์ ๊ฐ ๋ก๊ทธ์ธ์ ์๋ฃํ๊ฒ ๋๋ฉด ์๋ฒ๋ก๋ถํฐ ์ ์ ์ ์ ๋ณด๊ฐ response๋ก ๋ค์ด์ค๊ฒ ๋๊ณ ํด๋ผ์ด์ธํธ์์๋ ์ด ๊ฐ์ store์ ์ ์ฅํด์ ํ์ ์์ ๊บผ๋ด ์ธ ์ ์๋๋ฐ, ๊ธฐ์กด์ store๋ ์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ๋๋ ์ ๋ณด์ด๋ฏ๋ก ์ด๋ฅผ ํด๊ฒฐํด ์ฃผ์ด์ผ ํ๋ค.
redux-persist ์ค์น
npm i redux-persist
yarn add redux-persist
persist store ์ ์
store.tsx
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { useDispatch, useSelector } from "react-redux";
import storage from "redux-persist/lib/storage";
import { persistReducer } from "redux-persist";
.
.
.
import userSlice from "./modules/user";
const reducers = combineReducers({
.
.
.
user: userSlice.reducer,
});
// config ์์ฑ
const persistConfig = {
key: "root",
storage, // ๋ก์ปฌ ์คํ ๋ฆฌ์ง์ ์ ์ฅ
whitelist: ["user"],
};
const persistedReducer = persistReducer(persistConfig, reducers);
const store = configureStore({
reducer: persistedReducer,
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch();
export const useAppSelector = useSelector;
export default store;
main.tsx
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "/src/styles/main.css";
import { Provider } from "react-redux";
import store from "./store/store";
import { persistStore } from "redux-persist";
import { PersistGate } from "redux-persist/integration/react";
export const persistor = persistStore(store);
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
);
non-serializable value error
์ง๋ ฌํ๋ redux์์ ๊ฐ์ ์ฃผ๊ณ ๋ฐ์ ๋ object ํํ์ ๊ฐ์ string ํํ๋ก ๋ณํํ๋ ๊ฒ์ ๋งํ๋ค.
๋ฆฌ๋์ค ๋ฏธ๋ค์จ์ด๋ dispatch ๋ ์ก์
์ด ๋ฆฌ๋์์ ๋๋ฌํ๊ธฐ ์ ์ค๊ฐ ์์ญ์์ ์ฌ์ฉ์์ ๋ชฉ์ ์ ๋ง๊ฒ ๊ธฐ๋ฅ์ ํ์ฅํ ์ ์๋๋ก ๋๋๋ฐ, ๊ธฐ๋ณธ ๋ฏธ๋ค์จ์ด (getDefaultMiddleware)๋ ๋ถ๋ณ์ฑ ๊ฒ์ฌ์ ์ง๋ ฌํ ๊ฐ๋ฅ์ฑ ๊ฒ์ฌ(์ง๋ ฌํํ ์ ์๋ ๊ฐ์ฒด๋ฅผ ๋ฐ์์ ๋ error๋ฅผ ๋ฐ์)๋ฅผ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ redux-persist๊ฐ ํด์ผ ํ๋ ์ผ๊ณผ ์ถฉ๋์ด ์๊ธด ๊ฒ์ด๋ค!! (์ธ๋ฐ ์๋ ์๋ ์์..)
refer
https://github.com/rt2zz/redux-persist/issues/988
npm i @types/redux-logger
npm i redux-thunk
// ๋ณ๊ฒฝ ์
middleware: (getDefaultMiddleware) => getDefaultMiddleware()
// ๋ณ๊ฒฝ ํ
const store = configureStore({
reducer: persistedReducer,
middleware: [thunk, logger],
});
์๋ก๊ณ ์นจํด๋ ์ด๊ธฐํ ์ ๋๊ณ ๋ฉ์ง๊ฒ ์ ์ ์ฅ๋จ
'mitmitwiki' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ด๋ฒ ์ด์ค๋ก ๋ฌดํ ์คํฌ๋กค ๊ตฌํํ๊ธฐ (1) | 2023.04.08 |
---|---|
ํ์ด์ด๋ฒ ์ด์ค๋ก ํค์๋ ๊ฒ์ ๊ตฌํํ๊ธฐ (0) | 2023.04.07 |
Proxy ๊ทธ๊ฒ ๋ชฌ๋ฐ ์ด๋ก์ผ ํ๋ ๊ฑด๋ฐ (Vite Proxy ์ค์ ) (0) | 2023.04.02 |
axios interceptors๋ก 401 error ์ฒ๋ฆฌ (0) | 2023.04.02 |
PWA ์ ์ฉ ๋์ ใฑใ ฃ...! (0) | 2023.04.02 |