| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 |
- ํฅ์ค์ฝ๋
- textshadow
- selectorslevel4
- ํ์ ์ ๋ ํฐ
- ๊ทธ๋ฃน์ ๋ ํฐ
- texttransform
- bordercollapse
- ํฐํธ์จ์ดํธ
- borderspacing
- ๋นํ๊ทธ
- liststyleposition
- ์ ๋๋ฒ์ ์ ๋ ํฐ
- gtammertable
- formtag
- ์ฃผ์
- wordspacing
- liststyleimage
- ๋ชจ๋์นํฐํธ
- fontweight
- ํผํ๊ทธ
- captionside
- CDN
- ์์ด๋์ ๋ ํฐ
- borderstyle
- ์ถ์ฝ
- liststyletype
- ๊ตฌ๊ธํฐํธ
- ํด๋์ค์ ๋ ํฐ
- textdecoration
- inlineblock
- Today
- Total
็ก์
reducer์ reducers ๋ณธ๋ฌธ
๐ก reducer์ reducers
Redux Toolkit์ ์ฌ์ฉํ ๋ reducer(๋จ์)์ reducers(๋ณต์)๋ ์ด๋ฆ์ ๋น์ทํ์ง๋ง ์๋ฏธ๊ฐ ๋ค๋ฅด๋ค.
โผ๏ธ reducer (๋จ์)
reducer๋ ํ์ฌ State์ Action์ ๋ฐ์ ์๋ก์ด State๋ฅผ ๋ฐํํ๋ ํ๋์ ํจ์์ด๋ค.
Redux์์ ์ํ ๋ณ๊ฒฝ์ ๋ด๋นํ๋ ํต์ฌ ํจ์์ด๋ฉฐ, createSlice()๋ก ์์ฑ๋ ์ต์ข Reducer๋ reducer์ด๋ค.
(state, action) => newState
- Slice๋ฅผ ์์ฑํ๋ฉด ์ต์ข ์ ์ผ๋ก ํ๋์ reducer๊ฐ ๋ง๋ค์ด์ง๋ค.
export default counterSlice.reducer;
โผ๏ธ reducers (๋ณต์)
reducers๋ createSlice() ๋ด๋ถ์์ ์ฌ๋ฌ ๊ฐ์ reducer ํจ์๋ฅผ ์ ์ํ๋ ๊ฐ์ฒด์ด๋ค.
๊ฐ ํจ์๋ ํ๋์ Action๊ณผ ์ฐ๊ฒฐ๋๋ฉฐ, Redux Toolkit์ด Action๊ณผ Reducer๋ฅผ ์๋์ผ๋ก ์์ฑํด ์ค๋ค.
const counterSlice = createSlice({
name: "counter",
initialState: {
value: 0,
},
reducers: {
increment(state) {
state.value++;
},
decrement(state) {
state.value--;
},
},
});
- increment(): ํ๋์ reducer ํจ์.
- decrement(): ํ๋์ reducer ํจ์.
- reducers: reducer ํจ์๋ค์ ๋ชจ์๋๋ ๊ฐ์ฒด.
โผ๏ธ ๊ด๊ณ
reducers์ ์ ์๋ ์ฌ๋ฌ ๊ฐ์ reducer ํจ์๋ฅผ Redux Toolkit์ด ํ๋๋ก ํฉ์ณ ์ต์ข reducer๋ฅผ ์์ฑํ๋ค.
const counterSlice = createSlice({
reducers: {
increment() {},
decrement() {},
reset() {},
},
});
export default counterSlice.reducer;
- reducers: ์ฌ๋ฌ ๊ฐ์ reducer๋ฅผ ์ ์ํ๋ ๊ณณ.
- reducer: reducers๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ ์ต์ข Reducer.
๐ POINT ๐
โ๏ธ reducer๋ ํ๋์ ์ํ ๋ณ๊ฒฝ ํจ์์ด๋ค. โ๏ธ
โ๏ธ reducers๋ ์ฌ๋ฌ ๊ฐ์ reducer ํจ์๋ฅผ ์ ์ํ๋ ๊ฐ์ฒด์ด๋ค. โ๏ธ
โ๏ธ createSlice()๋ reducers๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋์ reducer๋ฅผ ์๋ ์์ฑํ๋ค. โ๏ธ
๐ reducer vs reducers ๐
| ๊ตฌ๋ถ | reducer | reducers |
| ์๋ฏธ | State๋ฅผ ๋ณ๊ฒฝํ๋ ํ๋์ ํจ์ | ์ฌ๋ฌ reducer ํจ์๋ฅผ ์ ์ํ๋ ๊ฐ์ฒด |
| ์ฌ์ฉ ์์น | counterSlice.reducer | createSlice() ๋ด๋ถ |
| ๊ฐ์ | 1๊ฐ | ์ฌ๋ฌ ๊ฐ |
| ์ญํ | State ๋ณ๊ฒฝ | State ๋ณ๊ฒฝ ํจ์๋ค์ ์ ์ |
โ๏ธ Redux Toolkit์ createSlice ์ฌ์ฉ โ๏ธ
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter', // ์ด slice์ ์ด๋ฆ
initialState: { value: 0 }, // ์ด๊ธฐ ์ํ
reducers: { // <-- ์ฌ๊ธฐ์ 'reducers' (๋ณต์)๋ฅผ ์๋๋ค!
increment: (state) => { // 'increment'๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ํ๋์ 'reducer' (๋จ์) ํจ์
state.value += 1;
},
decrement: (state) => { // 'decrement'๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ๋ ๋ค๋ฅธ ํ๋์ 'reducer' (๋จ์) ํจ์
state.value -= 1;
},
// ... ๋ ๋ง์ ์์ reducer ํจ์๋ค
},
// extraReducers: (builder) => { /* ์ธ๋ถ ์ก์
์ฒ๋ฆฌ */ }
});
// ์ด๋ ๊ฒ ์ ์๋ 'reducers' ๊ฐ์ฒด ์์ ์๋ 'increment'์ 'decrement'๊ฐ
// ๊ฐ๊ฐ ํ๋์ reducer ํจ์๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
export const { increment, decrement } = counterSlice.actions;
// ์ต์ข
์ ์ผ๋ก ์ธ๋ถ๋ก export๋ ๋,
// counterSlice.reducer๋ 'reducers' ์์ ์ ์๋ ๋ชจ๋ ์์ reducer ํจ์๋ค์
// ํ๋๋ก ํฉ์ณ์ง ๊ฑฐ๋ํ 'reducer' (๋จ์) ํจ์๊ฐ ๋ฉ๋๋ค.
export default counterSlice.reducer;
'<React>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| reducer / Redux / Redux Toolkit (0) | 2026.07.20 |
|---|---|
| Redux Toolkit (๋ฆฌ๋์ค ํดํท) (0) | 2026.07.16 |
| Redux (๋ฆฌ๋์ค) (0) | 2026.07.16 |
| useReducer (์ ์ฆ๋ฆฌ๋์) (0) | 2026.07.15 |
| Context API (์ฝํ ์คํธ ์์ดํผ์์ด) (0) | 2026.07.15 |
