Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

redux-persist ์ ์šฉ (feat. react, redux-toolkit) ๋ณธ๋ฌธ

mitmitwiki

redux-persist ์ ์šฉ (feat. react, redux-toolkit)

kohi โ˜• 2023. 3. 28. 18:03

 

 

๐Ÿคฏ ์œ ์ €๊ฐ€ ๋กœ๊ทธ์ธ์„ ์™„๋ฃŒํ•˜๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์œ ์ €์˜ ์ •๋ณด๊ฐ€ 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

 

non-serializable value error · Issue #988 · rt2zz/redux-persist

Hi folks ๐Ÿ‘‹, I just installed the redux-persist in a project still in its early stages and I'm facing this error message in the console as first thing when the page loads. I haven't done much beside...

github.com

 

npm i @types/redux-logger
npm i redux-thunk
// ๋ณ€๊ฒฝ ์ „
middleware: (getDefaultMiddleware) => getDefaultMiddleware()

// ๋ณ€๊ฒฝ ํ›„
const store = configureStore({
    reducer: persistedReducer,
    middleware: [thunk, logger],
});

 

์ƒˆ๋กœ๊ณ ์นจํ•ด๋„ ์ดˆ๊ธฐํ™” ์•ˆ ๋˜๊ณ  ๋ฉ‹์ง€๊ฒŒ ์ž˜ ์ €์žฅ๋จ