티스토리 뷰
redux를 이용하는데 새로고침을 하면 state가 계속 날아가서 답답해하던 중, 알게 된 것이다. 이것만 있으면 새로고침도 두렵지 않다.
- redux-persist를 설치한다 (예: npm i redux-persist)
- root reducer를 persistConfig 설정한다.
- react의 index.js에 적용한다.
// 2번 과정
import { combineReducers } from "redux";
import userReducer from "./userReducer";
import storage from "redux-persist/lib/storage"; // localStorage를 이용한다고 한다.
import { persistReducer } from "redux-persist";
// import storageSession from 'redux-persist/lib/storage/session' -> session storage를 이용한다고 한다.
const persistConfig = {
key: "root", // localStorage key
storage, // localStorage
whitelist: ["userReducer"], // target (reducer name)
};
const rootReducer = combineReducers({ userReducer });
export default persistReducer(persistConfig, rootReducer);
// 3번 과정
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { createGlobalStyle } from "styled-components";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import { store, persistor } from "./redux/store/store"; // V
import { PersistGate } from "redux-persist/integration/react"; // V
const GlobalStyle = createGlobalStyle`...`;
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<BrowserRouter>
<GlobalStyle />
<Provider store={store}>
<PersistGate persistor={persistor}> // persistGate 설정
<App />
</PersistGate>
</Provider>
</BrowserRouter>
);
'기록 > 개인' 카테고리의 다른 글
preproject ~04/20 (0) | 2023.04.20 |
---|---|
preproject ~ 04/19 (0) | 2023.04.19 |
github 에러난 거 기록 (계속 갱신됨) (0) | 2023.04.19 |
엄청 짧음) C++ 문자열 파싱 함수 istringstream (0) | 2023.03.27 |
유튜브같이 동영상 보는 거 ~ 2023/03/13 (0) | 2023.03.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 다이나믹프로그래밍
- 알고리즘
- react
- 리액트
- 프로그래머스
- 백준
- CSS
- 동적계획법
- async
- 타입스크립트
- aws
- 햄버거버튼
- NextJS
- 브루트포스
- 비트마스킹
- C++
- Redux
- SQL
- BFS
- themoviedb
- 스택
- 넥스트js
- Next.js
- 카카오맵
- 코드스테이츠
- 구현
- 자바스크립트
- 완전탐색
- 순열
- typescript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함