ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฏ useReducer๋ ๋ฌด์์ผ๊น์?
useReducer๋ ์ํ(state)๋ฅผ ๊ด๋ฆฌํ๋ ๋๊ตฌ์์.
React์์๋ ๋ณดํต **useState**๋ฅผ ์ฌ์ฉํด์ ์ํ๋ฅผ ๊ด๋ฆฌํ์ง๋ง, ์ํ๊ฐ ๋ณต์กํด์ง๋ฉด ์ฝ๋๊ฐ ์ง์ ๋ถํด์ง ์ ์์ด์.
์ด๋ด ๋ **useReducer**๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ๋ฅผ ๋ ์ฒด๊ณ์ ์ด๊ณ ๊น๋ํ๊ฒ ๊ด๋ฆฌํ ์ ์์ด์.
๐งฉ useState์ useReducer ๋น๊ตํ๊ธฐ
๋จผ์ useState๋ฅผ ๊ฐ๋จํ๊ฒ ๋ณต์ตํด๋ณผ๊น์?
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>์นด์ดํธ: {count}</p>
<button onClick={() => setCount(count + 1)}>์ฆ๊ฐ</button>
<button onClick={() => setCount(count - 1)}>๊ฐ์</button>
</div>
);
}
์ด ์ฝ๋์์๋
- useState๋ฅผ ์ฌ์ฉํด์ ์ซ์ ํ๋๋ง ๊ด๋ฆฌํด์.
- ๊ฐ๋จํ๋๊น useState๋ก ์ถฉ๋ถํ์ฃ .
ํ์ง๋ง ์ํ๊ฐ ๋ณต์กํด์ง๊ฑฐ๋ ๋ค์ํ ์ก์
์ ์ฒ๋ฆฌํด์ผ ํ๋ค๋ฉด?
์ฌ๊ธฐ์ **useReducer**๊ฐ ๋ฑ์ฅํฉ๋๋ค! ๐
๐ ๏ธ useReducer ๊ธฐ๋ณธ ์์
**useReducer**๋ฅผ ์ฌ์ฉํ๋ฉด ์ํ์ ๊ทธ ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง๋ฅผ ๋ช
ํํ๊ฒ ์ ์ํ ์ ์์ด์.
๋จผ์ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ์์๋ณผ๊น์?
1. useReducer ๊ธฐ๋ณธ ๋ฌธ๋ฒ
const [state, dispatch] = useReducer(reducer, initialState);
- reducer: ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ์ ์ํ๋ ํจ์์์.
- initialState: ์ํ์ ์ด๊ธฐ๊ฐ์ด์์.
- dispatch: ์ํ๋ฅผ ๋ฐ๊ฟ ๋ ์คํํ๋ ํจ์์์.
2. ๊ฐ๋จํ ์์ : ์นด์ดํฐ ๋ง๋ค๊ธฐ
์๊น useState๋ก ๋ง๋ค์๋ ์นด์ดํฐ๋ฅผ useReducer๋ก ๋ค์ ๋ง๋ค์ด ๋ณผ๊ฒ์!
import React, { useReducer } from "react";
// ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ์ ์ํ๋ reducer ํจ์
function reducer(state, action) {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
case "DECREMENT":
return { count: state.count - 1 };
default:
return state;
}
}
function Counter() {
const initialState = { count: 0 }; // ์ด๊ธฐ ์ํ
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<p>์นด์ดํธ: {state.count}</p>
<button onClick={() => dispatch({ type: "INCREMENT" })}>์ฆ๊ฐ</button>
<button onClick={() => dispatch({ type: "DECREMENT" })}>๊ฐ์</button>
</div>
);
}
export default Counter;
๐ ์ฝ๋ ์ค๋ช
- reducer ํจ์
- reducer๋ ์ํ(state)๋ฅผ ๋ณ๊ฒฝํ๋ ๋ก์ง์ ๋ชจ์๋์ ํจ์์์.
- action.type์ ๋ฐ๋ผ ์ด๋ค ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ๊ฒฐ์ ํด์.
- ์๋ฅผ ๋ค์ด:
- INCREMENT: count๋ฅผ +1 ์ฆ๊ฐ์์ผ์.
- DECREMENT: count๋ฅผ -1 ๊ฐ์์์ผ์.
- useReducer ํ
- useReducer๋ฅผ ์ฌ์ฉํด์ state(ํ์ฌ ์ํ)์ dispatch(์ํ๋ฅผ ๋ณ๊ฒฝํ๋ ํจ์)๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
- dispatch ํจ์
- dispatch({ type: "INCREMENT" })์ฒ๋ผ ํธ์ถํด์ ์ํ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
- ์ด๋ type์ ์ด๋ค ์ก์ ์ธ์ง ๋ํ๋ด๋ ์ญํ ์ ํด์.
๐ ์์ ์ด์ผ๊ธฐ๋ก ์ดํดํ๊ธฐ
์ฌ๋ฌ๋ถ์ด ํผ์ ๊ฐ๊ฒ๋ฅผ ์ด์ํ๋ค๊ณ ์๊ฐํด ๋ด์. ๐
- state: ๊ฐ๊ฒ์ ์ํ (์: ํผ์ ์๋ 10๊ฐ)
- action: ์ด๋ค ํ๋์ด ์ผ์ด๋ ์ง (์: "ํผ์ ์ถ๊ฐ" ๋๋ "ํผ์ ํ๋งค")
- reducer: ์ด ํ๋์ ๋ฐ๋ผ ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ์ ์ํ๋ ๊ณณ์ด์์.
ํผ์ ๊ฐ๊ฒ์ ๋ค์ด์จ ์ฃผ๋ฌธ(์ก์
)์ dispatch์ ์ ๋ฌํ๋ฉด,
reducer๊ฐ "ํผ์๋ฅผ ํ๋ ๋ ์ถ๊ฐํ ์ง" ์๋๋ฉด "ํ๋๋ฅผ ํ๋งคํ ์ง" ๊ฒฐ์ ์ ๋ด๋ ค์.
๐งฉ useReducer๋ฅผ ์ธ์ ์ฌ์ฉํ ๊น์?
- ์ํ๊ฐ ๋ณต์กํ๊ฑฐ๋ ์ฌ๋ฌ ์ข
๋ฅ์ ๊ฐ์ ๊ด๋ฆฌํ ๋
- ์: ํ ์ผ ๋ชฉ๋ก ๊ด๋ฆฌ, ์ผํ๋ชฐ ์ฅ๋ฐ๊ตฌ๋ ๋ฑ
- ์ํ ๋ณ๊ฒฝ ๋ก์ง์ด ํ๊ณณ์ ๋ชจ์ฌ ์์ด์ผ ํ ๋
- ๋ชจ๋ ์ํ ๋ณ๊ฒฝ ๋ก์ง์ด reducer ์์ ๋ชจ์ฌ ์์ผ๋๊น ์ ์ง๋ณด์๊ฐ ์ฌ์์ ธ์.
- ์ฌ๋ฌ ์ก์
์ ์ฒ๋ฆฌํด์ผ ํ ๋
- ์ํ๋ฅผ ๋ฐ๊ฟ ๋ ์ฌ๋ฌ ๊ฐ์ง ๋์(INCREMENT, DECREMENT, RESET ๋ฑ)์ ์ฒ๋ฆฌํด์ผ ํ๋ค๋ฉด useReducer๊ฐ ๊น๋ํด์.
๐ ์ ๋ฆฌํ์๋ฉด!
useReducer๋ ์ํ๋ฅผ ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ , ์ํ๋ฅผ ์ด๋ป๊ฒ ๋ฐ๊ฟ์ง ๋ฏธ๋ฆฌ ์ ์ํ๋ ๋๊ตฌ์์.
๋ณต์กํ ์ํ์ ์ฌ๋ฌ ์ก์
์ด ํ์ํ ์ํฉ์์ useState๋ณด๋ค ๋ ๊ฐ๋ ฅํ๊ณ ๊น๋ํ ํด๊ฒฐ์ฑ
์ด ๋ฉ๋๋ค.
'web > react hook' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
useState (0) | 2024.12.14 |
---|---|
useRef (0) | 2024.12.14 |
useOptimistic (0) | 2024.12.14 |
useMemo (2) | 2024.12.14 |
useLayoutEffect (0) | 2024.12.14 |
- Total
- Today
- Yesterday
- react dom
- state
- experimental
- Props
- react
- flushsync
- react dom hook
- useinsertioneffect
- experimental_taintuniquevalue
- ์ ์ฅ์
- react hook
- experimental_taintobjectreference
- preconnect
- Nextjs
- Store
- ์ธ๋ถํฐํธ
- finddomnode
- usedeferredvalue
- dns-prefetch
- ์ต์ ๋ฐ์ดํฐ
- zustand
- useid
- ๋ฐ์ดํฐ์ ์ง
- prefetchdns
- ์ํ๊ด๋ฆฌ
- ์คํ์
- component
- ๋ฒ ํ
- useRef
- useformstatus
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |