| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
- ํผํ๊ทธ
- CDN
- ๊ตฌ๊ธํฐํธ
- ํ์ ์ ๋ ํฐ
- ๊ทธ๋ฃน์ ๋ ํฐ
- selectorslevel4
- textdecoration
- texttransform
- ํฐํธ์จ์ดํธ
- formtag
- bordercollapse
- liststyleposition
- textshadow
- ํฅ์ค์ฝ๋
- ๋ชจ๋์นํฐํธ
- ์ถ์ฝ
- ๋นํ๊ทธ
- borderstyle
- inlineblock
- gtammertable
- fontweight
- wordspacing
- captionside
- borderspacing
- ์ฃผ์
- liststyleimage
- ํด๋์ค์ ๋ ํฐ
- ์ ๋๋ฒ์ ์ ๋ ํฐ
- liststyletype
- ์์ด๋์ ๋ ํฐ
- Today
- Total
็ก์
Redux Toolkit (๋ฆฌ๋์ค ํดํท) ๋ณธ๋ฌธ
๐ก Redux Toolkit (RTK)
Redux๋ฅผ ์ฝ๊ณ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ๋ง๋ Redux ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ.
โผ๏ธ ํ์ ์ด์
๊ธฐ์กด Redux๋ ์ํ ํ๋๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํด Store ์ค์ , Action ์์ฑ, Reducer ์์ฑ ๋ฑ์ ๊ฐ๊ฐ ์์ฑํด์ผํด ๋ฐ๋ณต๋๋ ์ฝ๋๊ฐ ๋ง๊ณ ์ค์ ์ด ๋ณต์กํ๋ค.
Redux Toolkit์ ์ด๋ฌํ ๊ณผ์ ์ ๋จ์ํํ์ฌ ๋ ์ ์ ์ฝ๋๋ก Redux๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ค๋ค.
โผ๏ธ ์ฃผ์ ํน์ง
- ๋ฐ๋ณต ์ฝ๋(Boilerplate) ๊ฐ์
- Action, Reducer, Store ์ค์ ๋ฑ์ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค.
- Store ์ค์ ๊ฐ์ํ
- configureStore()๋ฅผ ํตํด Store๋ฅผ ์ฝ๊ฒ ์์ฑํ ์ ์๋ค.
- Action๊ณผ Reducer ์๋ ์์ฑ
- createSlice() ํ๋๋ก State, Action, Reducer๋ฅผ ํจ๊ป ์์ฑํ๋ค.
- Redux DevTools ๊ธฐ๋ณธ ์ง์
- ๋ณ๋์ ์ค์ ์์ด DevTools๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- Redux Thunk ๊ธฐ๋ณธ ํฌํจ
- ๋น๋๊ธฐ ๋ก์ง์ ์ถ๊ฐ ์ค์ ์์ด ์ฌ์ฉํ ์ ์๋ค.
- Immer ์ง์
- State๋ฅผ ์ง์ ์์ ํ๋ ๊ฒ์ฒ๋ผ ์์ฑํด๋ ๋ถ๋ณ์ฑ์ด ์ ์ง๋๋ค.
โผ๏ธ ์ฃผ์ ๊ธฐ๋ฅ
๐น configureStore()
Redux Store๋ฅผ ์์ฑํ๋ ํจ์๋ก, ์ฌ๋ฌ ๊ฐ์ Slice๋ฅผ ํ๋์ Store๋ก ํฉ์ณ ๊ด๋ฆฌํ ์ ์๋ค.
import { configureStore } from "@reduxjs/toolkit";
const store = configureStore({
reducer: {
counter: counterReducer,
user: userReducer,
},
});
- ํ๋์ Store์ ์ฌ๋ฌ Slice๋ฅผ ๋ฑ๋กํ์ฌ ์ ์ญ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค.
๐น createSlice()
Redux Toolkit์์ ๊ฐ์ฅ ํต์ฌ์ ์ธ API์ด๋ค.
ํ๋์ ํจ์์์ ๋ค์ ๋ด์ฉ์ ๋ชจ๋ ์์ฑํ๋ค.
- State
- Reducer
- Action
import { createSlice } from "@reduxjs/toolkit";
const counterSlice = createSlice({
name: "counter",
initialState: {
value: 0,
},
reducers: {
up(state) {
state.value++;
},
down(state) {
state.value--;
},
},
});
- ์์ฑ๋ Slice์์๋ Action๊ณผ Reducer๋ฅผ ์๋์ผ๋ก ๋ง๋ค์ด์ค๋ค.
export const { up, down } = counterSlice.actions;
export default counterSlice.reducer;
โผ๏ธ Store์ Slice์ ๊ด๊ณ
Redux Toolkit์์๋ Store ํ๋์ ์ฌ๋ฌ ๊ฐ์ Slice๋ฅผ ๋ฑ๋กํ์ฌ ๊ด๋ฆฌํ๋ ๋ฐฉ์์ด ์ผ๋ฐ์ ์ด๋ค.
Store
โโ counterSlice
โโ userSlice
โโ productSlice
โโ cartSlice
- ๊ฐ Slice๋ ํ๋์ ๊ธฐ๋ฅ์ด๋ ์ํ๋ฅผ ๋ด๋นํ๋ฉฐ, configureStore()๊ฐ ์ด๋ฅผ ํ๋์ Store๋ก ๊ด๋ฆฌํ๋ค.
โผ๏ธ Immer ์ง์
Redux์์๋ ๊ธฐ์กด State๋ฅผ ์ง์ ์์ ํ๋ฉด ์ ๋๋ค.
Redux Toolkit์ ๋ด๋ถ์ ์ผ๋ก Immer๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ง์ ์์ ํ๋ ๊ฒ์ฒ๋ผ ์์ฑํด๋ ์๋ก์ด State๋ฅผ ์์ ํ๊ฒ ์์ฑํ๋ค.
return {
...state,
value: state.value + 1,
};
- ๊ธฐ์กด Redux
state.value++;
- Redux Toolkit
- ์ค์ ๋ก๋ Immer๊ฐ ์๋ก์ด State๋ฅผ ์์ฑํด ์ฃผ๋ฏ๋ก Redux์ ๋ถ๋ณ์ฑ ๊ท์น์ ๊ทธ๋๋ก ์ ์ง๋๋ค.
โผ๏ธ React์ ํจ๊ป ์ฌ์ฉํ๋ API
Redux Toolkit์ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ณ , react-redux๋ React์ Redux๋ฅผ ์ฐ๊ฒฐํ๋ค.
| API | ์ญํ |
| Provider | Store๋ฅผ React์ ์ฐ๊ฒฐ |
| useSelector() | Store์ State ์กฐํ |
| useDispatch() | Action์ dispatch |
โผ๏ธ Redux์ Redux Toolkit ์ฐจ์ด
| Redux | Redux Toolkit |
| ์ค์ ์ด ๋ณต์กํจ | ์ค์ ์ด ๊ฐ๋จํจ |
| ๋ฐ๋ณต ์ฝ๋๊ฐ ๋ง์ | ๋ฐ๋ณต ์ฝ๋๊ฐ ์ ์ |
| Action์ ์ง์ ์์ฑ | Action ์๋ ์์ฑ |
| Reducer๋ฅผ ์ง์ ๊ด๋ฆฌ | createSlice๋ก ํ ๋ฒ์ ๊ด๋ฆฌ |
| Store ์ค์ ์ด ๋ณต์ก | configureStore๋ก ๊ฐ๋จํ๊ฒ ์์ฑ |
| ๋ถ๋ณ์ฑ์ ์ง์ ๊ด๋ฆฌ | Immer๋ก ์๋ ๊ด๋ฆฌ |
๐ POINT ๐
โ๏ธ Redux Toolkit์ Store ์์ฑ, Reducer ์์ฑ, Action ์์ฑ ๋ฑ์ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์์ผ๋ฉฐ, Redux ๊ณต์ ๊ถ์ฅ ๋ฐฉ์์ด๋ค. โ๏ธ
โ๏ธ ๋ํ์ ์ธ API๋ configureStore() ์ createSlice() ์ด๋ค.โ๏ธ
๐ Redux Toolkit ์ค์น ๐
1๏ธโฃ ํ๋ก์ ํธ ์์ฑํ๋ฉด์ Redux Toolkit ๊ฐ์ด ์ค์น
npx create-react-app my-app --template redux
2๏ธโฃ React ํ๋ก์ ํธ๋ฅผ ๋ง๋ ํ Redux Toolkit๊ณผ React-Redux ๊ฐ์ด ์ค์น
npm install @reduxjs/toolkit react-redux
3๏ธโฃ Redux์ React-Redux ์ค์น ํ Toolkit ๋ง ์ถ๊ฐ
npm install @reduxjs/toolkit
๐ ํ: React ๋ฒ์ ์ด ๋ฎ์์ ์๊ธฐ๋ ์ค๋ฅ
Redux Toolkit ์ค์น ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ๋จผ์ package.json์ ํ์ธํ ๊ฒ.
"react": "^18.1.x",
"react-dom": "^18.1.x"
- ์ค๋ฌด์์ ์ค๋๋ ํ๋ก์ ํธ๋ฅผ ์ ์ง๋ณด์ํ ๋ ์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ๋ก React์ react-dom ๋ฒ์ ์ด ๋๋ฌด ๋ฎ์ผ๋ฉด ํธํ์ฑ ๋ฌธ์ ๋ก ์ค์น ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์๋ค. ํ์ํ๋ค๋ฉด ํ๋ก์ ํธ ํ๊ฒฝ์ ๋ง๊ฒ React ๋ฒ์ ์ ์ ๊ทธ๋ ์ด๋ํ๋ฉด ํด๊ฒฐ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค. (ํญ๋ชฉ์ ๋ฒ์ ์ด 18.1.x ์ด์์ด์ด์ผ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.)
โ Redux Toolkit์ ์ค์นํ๋ฉด redux๋ ๋ฐ๋ก ์ค์นํ ํ์๊ฐ ์๋ค. โ
- Redux Toolkit (@reduxjs/toolkit): Redux ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ก์ง(์ก์ , ๋ฆฌ๋์, ์คํ ์ด ์ค์ )์ ์์ฑํ๋ ๋ฐ ํ์ํ ๋๊ตฌ๋ค์ ๋ชจ์์ด๋ค. Redux ์ฝ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฏ๋ก redux ํจํค์ง๋ฅผ ๋ฐ๋ก ์ค์นํ ํ์๊ฐ ์๋ค.
- React-Redux (react-redux): React ์ปดํฌ๋ํธ์ Redux ์คํ ์ด๋ฅผ ์ฐ๊ฒฐํ๋ ๋ฐ ํ์ํ ์ญํ ์ ํ๋ค. Redux ์คํ ์ด์ ์ํ๋ฅผ React ์ปดํฌ๋ํธ์ ์ฃผ์ ํ๊ณ , ์ปดํฌ๋ํธ์์ ์ก์ ์ ๋์คํจ์นํ ์ ์๊ฒ ํด์ฃผ๋ Provider, useSelector, useDispatch ๊ฐ์ ํ ๋ค์ ์ ๊ณตํ๋ค. Redux Toolkit๊ณผ๋ ๋ณ๊ฐ์ ์ญํ ์ ์ํํ๋ ๋ ๋ฆฝ์ ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๐ ๋ค์ด์ด๊ทธ๋จ์ผ๋ก ์ดํดํ๊ธฐ ๐
์ต๊ณ ๋ฆฌ๋์ค์ผ ๊ณ ๋ง๋ค! Redux & Redux Toolkit ์์๋ณด๊ธฐ
๋ฆฌ๋์ค๋ ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋๋ก, ํ์ฌ๊น์ง ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๊ณ ์๋ค. ์ํ ๊ด๋ฆฌ๋ UI์ UX์ ๋ง๊ฒ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ฑฐ๋, ์๋ฒ์ ์ฃผ๊ณ ๋ฐ๋ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ์ ๋งํ๋ค.๊ฐ๋จํ ํ๋ก์
velog.io
'<React>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| reducer / Redux / Redux Toolkit (0) | 2026.07.20 |
|---|---|
| reducer์ reducers (0) | 2026.07.20 |
| Redux (๋ฆฌ๋์ค) (0) | 2026.07.16 |
| useReducer (์ ์ฆ๋ฆฌ๋์) (0) | 2026.07.15 |
| Context API (์ฝํ ์คํธ ์์ดํผ์์ด) (0) | 2026.07.15 |
