React - useReducer

ํƒ€๋‹ค๋‹ฅยท2024๋…„ 2์›” 6์ผ
0
post-thumbnail
post-custom-banner

๐Ÿ‘ปuseReducer๊ฐ€ ์•„์ฃผ ํ—ท๊ฐˆ๋ฆฐ๋‹ค. ์•Œ ๋“ฏ ํ•˜์ง€๋งŒ ๋ชจ๋ฅด๊ฒ ๋Š” ์ƒํƒœ๋ผ
useReducer๊ฐ€์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ๊ณต๋ถ€ํ•ด๋ณด์ž.

useReducer

โœ๏ธ **์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ hook ์„ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค.**
  • state๊ฐ€ ๋‹จ์ˆœํ•˜๋‹ค๋ฉด useState()๋ฅผ ์‚ฌ์šฉ
  • state๊ฐ€ ๋ณต์žกํ•˜๋‹ค๋ฉด useReducer()๋ฅผ ์‚ฌ์šฉ (๊ฐ์ฒด, ๋ฐฐ์—ด ๊ฐ™์ด ํ•˜์œ„ ์š”์†Œ๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ)

๐Ÿฑ useReducer

const [state, dispatch] = useReducer(reducer, initialValue, init);

  • state
    • ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ํ˜„์žฌ ์ƒํƒœ
  • dispatch
    • ์•ก์…˜์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜. reducer๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.
    • ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ state์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ผ์œผํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • reducer
    • state ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜.
    • ์‹ค์งˆ์ ์œผ๋กœ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์•ก์…˜์„ ๋ฐœ์ƒ ์‹œํ‚ค๋Š” ํ•จ์ˆ˜๋Š” dispatch ์ด๋‹ค. ๊ฒฐ๊ตญ **dispatch ๊ฐ€ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜๊ฐ€ reducer** ์ธ ๊ฒƒ!
    • ํ˜„์žฌ์˜ state์™€ action๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ, ๊ธฐ์กด์˜ state๋ฅผ ๋Œ€์ฒดํ•  ์ƒˆ๋กœ์šด state๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • initialValue : initialState ์ƒํƒœ์˜ ์ดˆ๊ธฐ๊ฐ’
  • init : ์ดˆ๊ธฐ ํ•จ์ˆ˜ ( ์ƒ๋žต ๊ฐ€๋Šฅ)

๐Ÿฑ action

  • ์—…๋ฐ์ดํŠธ๋ฅผ ์œ„ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋‹ค.
  • dispatch์˜ ์ธ์ž๊ฐ€ ๋œ๋‹ค.
  • reducerํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ action์— ํ• ๋‹น๋œ๋‹ค.
    • function reducer(state, action){ โ€ฆ

๐Ÿฑ dispatch ํ•จ์ˆ˜

  • reducerํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.
  • disspatchํ•จ์ˆ˜์˜ ์ธ์ž์ธ action์€ reducer ํ•จ์ˆ˜์˜ ๋‘๋ฒˆ์งธ ์ธ์ž์ธ action์— ํ• ๋‹น๋œ๋‹ค.
    • <button onClick={()=> dispatch({type:"decrement"})}>

๐Ÿฑ reducer ํ•จ์ˆ˜

  • dispatchํ•จ์ˆ˜์— ์˜ํ•ด ์‹คํ–‰๋œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€์—์„œ state๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
  • ๊ธฐ์กด์˜ state๋ฅผ ์ƒˆ๋กœ์šด state๋กœ ๋Œ€์ฒดํ•ด์ค€๋‹ค! ( ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ฎ์–ด์“ฐ์ง€ ์•Š๋Š”๋‹ค.)

๐Ÿฑ ์ „์ฒด ์‚ฌ์šฉ ์˜ˆ์‹œ

import React, {useReducer} from 'react';
import './style.css';

//์–˜๊ฐ€ reducer ํ•จ์ˆ˜
function reducer(state, action){
  // state๊ฐ’๊ณผ action์˜ type์„ ํ†ตํ•ด reducer๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—
  //ํ˜„์žฌ state ๊ฐ์ฒด์˜ count์—์„œ 1์„ ๋นผ๊ณ , ๋”ํ•˜๊ณ ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
  switch(action.type){
    case "decrement":
      return {count: state.count - 1};
  
    case "increment":
      return {count: state.count + 1};
  }
}
export default function App() {
  // useState๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋น„์Šทํ•œ ํ˜•ํƒœ.
  //[number, setNumber] ์—์„œ ๊ฒฐ๊ตญ ๋’ค์˜ ํ•จ์ˆ˜๊ฐ€ ์„ธํ„ฐ ํ•จ์ˆ˜์ด๊ณ  ์ƒํƒœ ๋ณ€๊ฒฝ์„ ํ•ด์ค€๋‹ค.
  //๊ทธ๋Ÿฌ๋‹ˆ ๋’ค์— ์•ก์…˜์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” dispatchํ•จ์ˆ˜๋ฅผ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด๋œ๋‹ค. 
  //๊ทธ๋ฆฌ๊ณ  useReducer๋กœ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”๊ฑฐ๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€์— ์ดˆ๊ธฐ๊ฐ’ ์ž‘์„ฑ.
  const [number, dispatch] = useReducer(reducer, {count:0});

  return (
<>
{/* state์˜ number์— ์žˆ๋Š” count๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— {number.count} */}
<h1>Count: {number.count}</h1>
{/* ํด๋ฆญ ์‹œ dispatchํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ง€์ •๋œ type์˜. */}
<button onClick={()=> dispatch({type:"decrement"})}> ๋นผ๊ธฐ </button>
<button onClick={()=> dispatch({type:"increment"})}> ๋”ํ•˜ํ•˜๊ธฐ </button>
</>
  );
}

๋„์›€ ๋ฐ›์€ ๋งํฌ1, ๋„์›€ ๋ฐ›์€ ๋งํฌ2

profile
ํ”„๋ก ํŠธ์—”๋“œ ใ„ฑH๋ฐœใ…ˆr ใ…ŽH๋ณดใ…ˆr - โ˜†
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€