728x90
๐ useReducer
- ๋ฆฌ์กํธ์ ์ํ(state) ๊ด๋ฆฌ ํ ์ด๋ค.
- useState ๋ณด๋ค ๋ณต์กํ ์ํ ๋ก์ง์ ๊ด๋ฆฌํ ๋ ์ฌ์ฉํจ.
- -----------------------------------------------------------------------------------------
- ๊ธฐ๋ณธ ๊ฐ๋
๋ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ
- const [state, dispatch] = useReducer(reducer, initalState);
- state : ํ์ฌ ์ํ ๊ฐ
- dispatch : ์ํ๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ํด action ์ ์ ๋ฌํ๋ ํจ์
- reducer : ์ํ๋ฅผ ์ ๋ฐ์ดํธํ๋ ๋ก์ง์ ๋ด์ ํจ์
- initialState : ์ด๊ธฐ ์ํ ๊ฐ --> ๊ฐ์ฒด ํํ๋ก ๋ฃ๊ธฐ!
- -----------------------------------------------------------------------------------------
- useReducer ์ ํต์ฌ์ State ๋ฅผ ํ ๊ณณ์ ๋ชจ์๋๊ณ ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ!!
- state : ์ฐฝ๊ณ ์์ ์ ์ฅ๋ ํ์ฌ ๋ฌผํ ์ํ
- dispath : ์ฐฝ๊ณ ๊ด๋ฆฌ์์๊ฒ ๋ณด๋ด๋ ์์ฒญ์
- action : ์์ฒญ์์ ๋ค์ด๊ฐ๋ ๊ตฌ์ฒด์ ์ธ ์๊ตฌ์ฌํญ (์ถ๊ณ , ์
๊ณ ๋ฑ๋ฑ)
- action์ dispath ์ ๋ด์์ ๋ณด๋ธ๋ค๊ณ ์๊ฐํ๋ฉด ๋จ.
- reducer : ์์ฒญ์๋ฅผ ๋ฐํ์ผ๋ก ์ค์ ์ฒ๋ฆฌํ๋ ์์
์ง์นจ์ ๋ฐ ์ค์ state ๋ฅผ ๋ณํ์ํค๊ณ , ์
/์ถ๊ณ ๋ฑ์ ์ค์ ์์
์ด ๋ฐ์ ์ฌ๊ณ ์๋์ ๋ณํ ๋ฑ์ ๋ฐ์์ํด.
- ๊ฐ์ฅ ํต์ฌ์ ์ธ ๋ก์ง์ด ๋ค์ด๊ฐ๋ ๋ถ๋ถ.
- dispatch({type : "OUT", payload : inputValue})
- ๊ฐ์ฒดํํ๋ก ๋ฃ์ผ๋ฉด ๋จ.
- action.type ,
action.payload ์ ๊ฐ์ด action.~ ์ผ๋ก ๊บผ๋ด๋ฉด ๋จ. - -----------------------------------------------------------------------------------------
- ๊ฐ์ ์ปดํฌ๋ํธ์์ useReducer ์ ์ฌ๋ฌ๋ฒ ์ฌ์ฉ ๊ฐ๋ฅํจ.
- ๊ฐ๊ฐ์ useReducer ํ๋ = ์์ ์ํ ๋จธ์ 1๊ฐ ๋ผ๊ณ ์๊ฐํ๋ฉด ๋จ.
-
๋๋ณด๊ธฐ์ปดํฌ๋ํธ
โโ reducer A (์นด์ดํฐ)
โโ reducer B (์ ์ )
โโ reducer C (๋ชจ๋ฌ)
-
- โญ๏ธโญ๏ธ ์๋ก ๋ค๋ฅธ ๊ด์ฌ์ฌ(state) ๋ฅผ ๋ถ๋ฆฌํ ๋. --> ์ฌ์ฉํ๋ฉด ์ข์.!
- โโ ์ํ๋ค์ด ๊ฐํ๊ฒ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฉด?? --> reducer๊ฐ ์๋ก ์ฝํ.
- --> ์ด๋ฐ ๊ฒฝ์ฐ๋ ํ๋์ reducer ๋ก ํฉ์น๋ ๊ฒ ์ข์.
- ex) order ๊ณผ payment ์ ๊ฐ์ด ์๋ก ์ฐ๊ด์ฑ์ด ๋์ ๊ฒฝ์ฐ๋ ๋ฐ๋ก ํด์ผ ํจ.
- -----------------------------------------------------------------------------------------
๐ ํ์ฝ๋
import { useReducer, useState } from "react";
import { createRoot } from "react-dom/client";
function warehouseReducer(state, action) {
/*
switch(action.type) {
case "IN" : return {...state, stock : state.stock + 1};
case "OUT" : return {...state, stock : Math.max(0, state.stock - 1)};
default : return state;
}
*/
switch(action.type) {
case "IN" : return {...state, stock : state.stock + action.payload};
case "OUT" : return {...state, stock : Math.max(0, state.stock - action.payload)};
default : return state;
}
}
function userReducer(user, action){
switch(action.setName){
case "Y" : {
console.log( action.name );
return { ...user, name : action.name }
};
default : return user;
}
}
function WareHouse() {
const [state, dispatch] = useReducer(warehouseReducer, { stock : 0, cnt : 1 });
const [inputValue, setInputValue] = useState(1);
const userInitalState = { name : "ํ๊ธธ๋", age : 20 }; // userReducer ์ ๋ฃ์ ์ด๊ธฐ๊ฐ ๊ฐ์ฒด!
const [user, dispatchUser] = useReducer(userReducer, userInitalState );
const handleChange = (e) => {
let targetId = e.target.id;
let targetVal = e.target.value;
switch(targetId){
case "stock" : {
const val = parseInt(targetVal, 10);
// 1. useState ์ด์ฉํด๋ณด๊ธฐ.
// setInputValue( isNaN(val) ? 0 : val );
// 2. useReducer ์ด์ฉํด๋ณด๊ธฐ.
warehouseReducer( {cnt : targetVal} );
console.log(state.cnt);
break;
}
case "name" : {
dispatchUser( {setName: "Y" ,name : targetVal} );
break;
}
default : { console.log(`err ---- ${targetId}`); }
};
}
return (
<div>
<h1>์ฐฝ๊ณ ์ฌ๊ณ : {state.stock}</h1>
<p>
<input id="stock" type="number" value={inputValue} onChange={handleChange} placeholder="์๋์
๋ ฅ" />
</p>
<button onClick={ () => dispatch({type : "IN", payload : state.cnt}) }>์
๊ณ </button>
<button onClick={ () => dispatch({type : "OUT", payload : state.cnt}) }>์ถ๊ณ </button>
<p>
<input id="name" type="text" value={user.name} onChange={handleChange} placeholder="์๋์
๋ ฅ" />
</p>
</div>
);
}
createRoot(document.getElementById('react4')).render(<WareHouse />);

๐ ?
- ใ ใ
๐ ?
- ใ ใ
์ถ์ฒ :
๊ฐ๋ฐ ๊ณต๋ถ๋ฅผ ์ํ ๋ธ๋ก๊ทธ ์ ๋๋ค.
์ค๋ฅ๊ฐ ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์!
๊ฐ์ฌํฉ๋๋ค.

728x90
'front > react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [react] React Hook 5 (Custom Hook) (0) | 2026.01.07 |
|---|---|
| [react] React Hook 4 (useCallback, useMemo) (0) | 2026.01.07 |
| [react] React Hook 2 (useContext, useRef) (0) | 2026.01.06 |
| [react] React Hooks 1 (useState, useEffect) (0) | 2026.01.04 |
| [react] React Transition (useTransition) (0) | 2026.01.04 |