반응형
Redux
- One way data flow
- multiple components issue
- Lifting state up
- Extract shared state from the component tree
- Immutable
- object / array
- ...spread
- Terminology
- action {type, payload}
- reducer (state, action) => newState
- store (state lives) created by passing reducer
- dispatch only way to update state (pass in an action object)
- selectors extract specific pieces of information from a store state
- Initial setup
- store created by using reducer function
- store calls root reducer once save initial state
- UI first renedered
- Updates
- Something happend / dispatch action
- stroe run reducer with prev state & current action save new state
- notifies all parts store has been updated / Each UI check update
- need to changed UI re-render
Redux | |
---|---|
전역 상태 관리 | vs 지역 상태 관리 |
단 방향 데이터(상태)흐름 | Flux |
구성요소 | Store, Reducer, Action, Selector |
- 설치하기
npm install react-redux
yarn add react-redux
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import TodoApp from './TodoApp';
import { Provider } from 'react-redux';
import store from './redux/store';
// As of React 18
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<Provider store={store}>
<TodoApp />
</Provider>
);
- 설치하기
npm install @reduxjs/toolkit
yarn add @reduxjs/toolkit
Redux 2 | |
---|---|
RTK | Redux 라이브러리들 조합 |
라이브러리들 | immer, saga, thunk, reselect |
Redux Dev Tools | Chrome extension |
useSelector()
npm install axios
Redux 3 | |
---|---|
Hooks | useSelector , useDispatch |
API 통신 | 비동기 작업(RTK-Query) |
Redux-Thunk | Action으로 API 요청/결과 Store에 반영 |
반응형
'개발 이야기 > Front-end' 카테고리의 다른 글
React 라이브러리 #5 (0) | 2023.03.30 |
---|---|
Redux & Redux toolkit (0) | 2023.03.29 |
Virtual Dom (0) | 2023.03.27 |
React 공식문서로 디테일 잡기 #7 (0) | 2023.03.26 |
React 라이브러리 #3 (0) | 2023.03.23 |