Redux
react global state management library
Redux
component에 종속되지 않고 global state management를 합니다. props지옥을 탈출할 수 있습니다.
Flux pattern
redux는 기본적으로 flux 패턴을 따릅니다.
Action -> Dispatch -> Store -> View
Installation
yarn add redux react-redux redux-devtools-extension @reduxjs/toolkitReducer definition
reducer는 꼭 불변성을 지켜야 합니다. 그래야 redux가 이전 상태와 바뀐 상태를 구분할 수 있습니다. 왜냐하면 참조 값이 바뀌었는지를 가지고 변경 여부를 판단하고, 바뀌었다고 판단해야 re-render가 일어납니다.
Root reducer definition
// libraries import { applyMiddleware, combineReducers, compose, createStore } from 'redux'; import { composeWithDevTools } from 'redux-devtools-extension'; // customs import branchManagementReducer from 'modules/branch/reducer/branchManagement.reducer'; const rootReducer = combineReducers({ branchManage: branchManagementReducer, }); const middleware = applyMiddleware(); const enhancer = process.env.NEXT_PUBLIC_ENV_NODE === 'production' ? compose(middleware) : composeWithDevTools(middleware); export const store = createStore(rootReducer, enhancer);detailed reducer definition
// store/counter.js const SET_DIFF = 'counter/SET_DIFF'; const INCREASE = 'counter/INCREASE'; const DECREASE = 'counter/DECREASE'; export const setDiff = diff => ({ type: SET_DIFF, diff }); export const increase = () => ({ type: INCREASE }); export const decrease = () => ({ type: DECREASE }); const initialState = { number: 0, diff: 1 }; export default function counter(state = initialState, action) { switch(action.type) { case SET_DIFF: return { ...state, diff: action.diff }; case INCREASE: return { ...state, number: state.number + state.diff }; case DECREASE: return { ...state, number: state.number - state.diff }; default: return state; } }전역 컴포넌트에 store 생성하고 반영하기
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import { Provider } from 'react-redux'; import store from 'store'; // import './excercise'; ReactDOM.render( <React.StrictMode> <Provider store={store}> <App /> </Provider> </React.StrictMode>, document.getElementById('root') ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals reportWebVitals();
Root reducer definition
detailed reducer definition
dispatch
```typescript
redux-persist는 browser storage에도 저장하여 새로고침하여도 데이터가 유실되지 않는 라이브러리 입니다.
Installation
Configure
redux-toolkit
Last updated
Was this helpful?