useReducer()

1. ๊ฐœ์š”

๋ฆฌ์•กํŠธ์—์„œ๋Š” ๋ณดํ†ต ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์œ„ํ•ด useState()ํ›…์„ ์‚ฌ์šฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ useReducer()ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

useReducer()ํ›…์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŠน๋ณ„ํ•œ ํŠน์ง•์€ ํ•œ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋กœ์ง ๋ถ€๋ถ„์„ ๊ทธ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์ปดํฌ๋„ŒํŠธ ๋ฐ”๊นฅ์— ์ž‘์„ฑ ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์‹ฌ์ง€์–ด ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ž‘์„ฑ ํ›„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ useReducer()ํ›…์— ๋Œ€ํ•ด useState()ํ›…๊ณผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์„ ์‹œ์ž‘ํ•˜์—ฌ ์‚ดํŽด๋ณด์ž.


2. useState() vs useReducer()

์–ธ์ œ useState()ํ›…์„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์–ธ์ œ useReducer()ํ›…์„ ์‚ฌ์šฉํ•˜๋Š”์ง€๋Š” ์ •ํ•ด์ง„ ๊ธฐ์ค€์€ ์—†์ง€๋งŒ ๋ณดํ†ต ์•„๋ž˜์˜ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‚˜๋‰˜์–ด state๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

  • useState()ํ›…์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

    1. ๊ด€๋ฆฌํ•ด์•ผ ํ•  state๊ฐ€ 1๊ฐœ์ผ ๊ฒฝ์šฐ

    2. ๊ทธ state๊ฐ€ ๋‹จ์ˆœํ•œ ์ˆซ์ž, ๋ฌธ์ž์—ด ๋˜๋Š” boolean๊ฐ’์ผ ๊ฒฝ์šฐ


  • useReducer()ํ›…์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

    1. ๊ด€๋ฆฌํ•ด์•ผ ํ•  state๊ฐ€ 1๊ฐœ ์ด์ƒ, ๋ณต์ˆ˜์ผ ๊ฒฝ์šฐ

    2. ํ˜น์€ ํ˜„์žฌ๋Š” ๋‹จ์ผ state๊ฐ’๋งŒ ๊ด€๋ฆฌํ•˜์ง€๋งŒ, ์ถ”ํ›„ ์œ ๋™์ ์ผ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ฒฝ์šฐ

    3. ์Šค์ผ€์ผ์ด ํฐ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ

    4. state์˜ ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•ด์งˆ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š” ๊ฒฝ์šฐ


3. useReducer()ํ›…์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

์•„๋ž˜๋Š” useReducer()ํ›…์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ด๋‹ค.

const [state, dispatch] = useReducer(reducer, initialArg, init);
  • state: ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ƒํƒœ

  • dispatch: reducer ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ state์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ผ์œผํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

  • reducer: (state, action) =-> newState์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง„๋‹ค. ์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€์—์„œ state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

  • initalArg: ์ดˆ๊ธฐ state

  • init: ์ดˆ๊ธฐ ํ•จ์ˆ˜


4. initialArg๋ฅผ ํ†ตํ•œ ์ดˆ๊ธฐ state๊ฐ’ ์„ค์ •

initialArg๋ฅผ ํ†ตํ•ด ์ดˆ๊ธฐ state๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด useCallback()์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ ์ œ์ฃผ๋„ ์—ฌํ–‰ ์ปดํฌ๋„ŒํŠธ ๋ฐ”ํƒ•์œผ๋กœ ์ƒˆ๋กญ๊ฒŒ ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

import { useState, useEffect } from "react";
import { useForm } from "react-hook-form";

const reducer = () => {};

const initialArg = {
  isSunny: true,
  gift: [],
};

function App() {
  const [state, dispatch] = useReducer(reducer, initialArg);
  return <div></div>;
}

export default App;

์œ„์™€ ๊ฐ™์ด ์šฐ์„  initialArg๋ฅผ ๋งŒ๋“ค๊ณ  App์ปดํฌ๋„ŒํŠธ์—์„œ๋Š” useReducer()ํ›…์„ ๋ถˆ๋Ÿฌ์™€ ๊ธฐ๋ณธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ž. reudecer()ํ•จ์ˆ˜๋Š” ์•„๋ž˜์—์„œ ๋‹ค๋ฃฌ๋‹ค.

๋จผ์ € ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ฒผ๋‹ค. useReducer()ํ›…์„ ํ†ตํ•ด ๋ถˆ๋Ÿฌ์˜จ state์—๋Š” ์–ด๋–ค ๊ฐ’์ด ์ €์žฅ๋˜์–ด ์žˆ์„๊นŒ? ์ฝ˜์†”์„ ์ฐ์–ด ๊ทธ ๋‚ด์šฉ์„ ํ™•์ธํ•˜์ž.

initialArg์—์„œ ์„ ์–ธํ•œ ๊ฐ์ฒด๊ฐ€ ๊ทธ๋Œ€๋กœ ์ฐํžˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


5. state๊ฐ’์„ ๊ฐ€์ ธ์™€ ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ

์ด๋ฒˆ์—” initialArg๊ฐ์ฒด์˜ gift๋ฐฐ์—ด์— "ํ•œ๋ผ๋ด‰"๊ณผ "์ดˆ์ฝœ๋ฆฟ"์„ ์ถ”๊ฐ€ํ•œ ํ›„ state๊ฐ’์„ ๊ฐ€์ ธ์™€ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์ž. ์•„๋ž˜์˜ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ ์ž‘์„ฑํ•ด๋ณด์ž.

import { useState, useReducer } from "react";
import { useForm } from "react-hook-form";

const reducer = () => {};

const initialArg = {
  isSunny: true,

  // gift์˜ ์ดˆ๊ธฐ ๊ฐ’ ๋ณ€๊ฒฝ
  gift: ["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"],
};

function App() {
  const [state, dispatch] = useReducer(reducer, initialArg);

  // ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ state์•ˆ์˜ isSunny์™€ gift๋ฅผ ๊ฐ€์ ธ์™”๋‹ค
  const { isSunny, gift } = state;
  const { register, handleSubmit, setValue } = useForm();

  return (
    <div>
      <div>
        <span>๊ธฐ๋…ํ’ˆ์„ ๊ฐ€์ง€๊ณ  ์ง‘์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์„๊นŒ์š”?</span>
        <button>๋‚ ์”จ ๋ฐ”๊พธ๊ธฐ</button>
        <div>
          {isSunny
            ? "๋‚ ์”จ๊ฐ€ ๋งค์šฐ ์ข‹๋„ค์š”! ๋น„ํ–‰๊ธฐ๊ฐ€ ์ด๋ฅ™ํ•  ์ˆ˜ ์žˆ์–ด์š” ๐Ÿ›ซ"
            : "ํƒœํ’์ด ์™”๋„ค์š”ใ… ใ…  ๋‚ด์ผ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค๋ด์š” โ›ˆ"}
        </div>
      </div>
      <form>
        <input {...register("gift")} placeholder="์‚ฌ๊ณ  ์‹ถ์€ ๊ธฐ๋…ํ’ˆ" />
        <input type="submit" value="๊ธฐ๋…ํ’ˆ ๊ณ ๋ฅด๊ธฐ" />
      </form>
      <div>๋‚˜์˜ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก: {gift.join(", ")}</div>
    </div>
  );
}

export default App;

state๋ฅผ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์„ ํ•˜์—ฌ isSunny์™€ gift์„ ๊ฐ€์ ธ์™”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ฐ’์„ ๋ฐ”ํƒ•์œผ๋กœ ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๊ณ  ์žˆ๋‹ค.

useCallback()์ฑ•ํ„ฐ์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ ์ œ์ฃผ๋„ ์—ฌํ–‰๊ณผ ๊ด€๋ จ๋œ ์ปดํฌ๋„ŒํŠธ๋‹ค. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ๋ณ€ํ•˜์ง€ ์•Š์•˜์œผ๋‹ˆ ๊ทธ๋Œ€๋กœ ๋ณต๋ถ™ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๊ฐ€ ์ง€์šฐ๋ฉด ๋œ๋‹ค.


6. state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•œ ์ฒซ ๋ฒˆ์งธ ๊ณผ์ •์ธ dispatchํ•จ์ˆ˜ ์ž‘์„ฑ

dispatchํ•จ์ˆ˜๋Š” state์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์ผ์œผํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ dispatchํ•จ์ˆ˜์˜ ์ธ์ž๋กœ์จ ์—…๋ฐ์ดํŠธ๋ฅผ ์œ„ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ aciton๋ฅผ ์ด์šฉํ•œ๋‹ค. ๋˜ํ•œ dispatchํ•จ์ˆ˜๋Š” ๋ฐ”๋กœ ์•„๋ž˜์—์„œ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” reducerํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

์ด๋ฒคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ dispatchํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

import { useState, useReducer } from "react";
import { useForm } from "react-hook-form";

const reducer = (state, action) => {
  console.log(state, action);
  return state;
};

const initialArg = {
  isSunny: true,
  gift: ["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"],
};

function App() {
  const [state, dispatch] = useReducer(reducer, initialArg);
  const { isSunny, gift } = state;
  const { register, handleSubmit, setValue } = useForm();

  // ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์‹คํ–‰ ๋  ํ•จ์ˆ˜
  const onClickChangeBtn = () => dispatch({ type: "changeWeather" });
  const onSubmitFrom = (data) => {
    const { gift } = data;
    dispatch({
      type: "submitGift",
      gift,
    });
    setValue("gift", "");
  };

  return (
    <div>
      <div>
        <span>๊ธฐ๋…ํ’ˆ์„ ๊ฐ€์ง€๊ณ  ์ง‘์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์„๊นŒ์š”?</span>
        <button onClick={onClickChangeBtn}>๋‚ ์”จ ๋ฐ”๊พธ๊ธฐ</button>
        <div>
          {isSunny
            ? "๋‚ ์”จ๊ฐ€ ๋งค์šฐ ์ข‹๋„ค์š”! ๋น„ํ–‰๊ธฐ๊ฐ€ ์ด๋ฅ™ํ•  ์ˆ˜ ์žˆ์–ด์š” ๐Ÿ›ซ"
            : "ํƒœํ’์ด ์™”๋„ค์š”ใ… ใ…  ๋‚ด์ผ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค๋ด์š” โ›ˆ"}
        </div>
      </div>
      <form onSubmit={handleSubmit(onSubmitFrom)}>
        <input {...register("gift")} placeholder="์‚ฌ๊ณ  ์‹ถ์€ ๊ธฐ๋…ํ’ˆ" />
        <input type="submit" value="๊ธฐ๋…ํ’ˆ ๊ณ ๋ฅด๊ธฐ" />
      </form>
      <div>๋‚˜์˜ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก: {gift.join(", ")}</div>
    </div>
  );
}

export default App;

buttonํƒœ๊ทธ์™€ formํƒœ๊ทธ์— ๊ฐ๊ฐ onClick๊ณผ onSubmit์ด๋ฒคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜์˜€๊ณ  onClickChangeBtn()ํ•จ์ˆ˜์™€ onSubmitFrom()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ดํŽด๋ณด์ž.

  • onClickChangeBtn(): dispatchํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์ธ์ž๋กœ ๊ฐ์ฒด{type: "changeWeather"}๊ฐ€ ์ „๋‹ฌ๋˜์—ˆ๋‹ค.

  • onSubmitFrom(): ์—ฌ๊ธฐ์„œ๋Š” dispatchํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๊ฐ์ฒด{ type: "submitGift", gift }๊ฐ€ ์ „๋‹ฌ ๋˜์—ˆ๋‹ค.


7. state๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๋ฒˆ์งธ ๊ณผ์ •์ธ reducerํ•จ์ˆ˜ ์ž‘์„ฑ

dispatchํ•จ์ˆ˜์˜ ์‹คํ–‰์œผ๋กœ reducerํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. reducerํ•จ์ˆ˜๋Š” ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ๋Š” ํ˜„์žฌ(๋ฐ”๋€Œ๊ธฐ ์ „)์˜ state์ด๊ณ  ๋‘ ๋ฒˆ์งธ๋Š” action์ด๋‹ค. ์ฆ‰ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

const reducer = (state, action) => {};

reudcerํ•จ์ˆ˜๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์—ฌ ์ „๋‹ฌ ๋ฐ›์€ state์™€ action๋ฅผ ์ฝ˜์†”๋กœ ์ฐ์–ด๋ณด์ž

const reducer = (state, action) => {
  console.log(state, aciton);
};

๋‚ ์”จ ๋ฐ”๊พธ๊ธฐ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €๋”๋‹ˆ ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค. ๋‹คํ–‰ํžˆ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” state์™€ action์€ ์ฝ˜์†”๋กœ ์ž˜ ์ฐํ˜”๋‹ค. ์—๋Ÿฌ๋ฅผ ํŒŒํ•ด์น˜๊ธฐ ์ „ ํ•ด๋‹น ๊ฐ’์„ ์‚ดํŽด๋ณด์ž.

  • state: ํ˜„์žฌ์˜ state

  • action: dispatchํ•จ์ˆ˜์— ์ธ์ž๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด

์—๋Ÿฌ๋Š” ๋„๋Œ€์ฒด ์™œ? ๋ฐœ์ƒํ•œ ๊ฒƒ์ผ๊นŒ? ๊ทธ ์ด์œ ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. useReducer()ํ›…์˜ reducerํ•จ์ˆ˜๋Š” state ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ๋‹น์—ฐํžˆ ์ธ์ž๋กœ ๋ฐ›์€ state๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ(๋ฌผ๋ก  ๊ฐ€๋Šฅ์€ ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด state๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” useReducer()ํ›…์˜ ์˜๋ฏธ๊ฐ€ ์—†์–ด์ง„๋‹ค.) ์–ด๋– ํ•œ ๋กœ์ง์„ ํ†ตํ•ด ๋ฐ”๋€Œ๋Š” state๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” ๋ฐ˜ํ™˜๋˜๋Š” state๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋‹ค. ์ž„์‹œ๋ฐฉํŽธ์œผ๋กœ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

const reducer = (state, action) => {
  console.log(state, aciton);
  return state;
};

reducerํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›๋Š” state์™€ action์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์œผ๋‹ˆ ์ด๋ฅผ ๊ฐ€์ง€๊ณ  state๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ž.

reducerํ•จ์ˆ˜๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ ์ž‘์„ฑํ•œ๋‹ค.

const reducer = (state, action) => {
  const { isSunny, gift } = state;
  switch (action.type) {
    case "changeWeather":
      return {
        ...state,
        isSunny: !isSunny,
      };
    case "submitGift":
      return {
        ...state,
        gift: [...gift, action.gift],
      };
    default:
      throw new Error("type Error");
  }
};
  1. ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น์œผ๋กœ state๊ฐ’์˜ ๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š” isSuuny์™€ gift์„ ๊ฐ€์ ธ์˜จ๋‹ค.

  2. action๊ฐ์ฒด์—๋Š” type์ด ์กด์žฌํ•˜๋ฉฐ ์ด๋Š” ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค. onSubmitForm์ด๋ฒคํŠธ์ธ ๊ฒฝ์šฐ type๋ฟ ์•„๋‹ˆ๋ผ ์„ ๋ฌผ์˜ ์ด๋ฆ„์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” gift๋„ ์กด์žฌํ•œ๋‹ค.

  3. switch๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ action.type๊ณผ ์ผ์น˜ํ•˜๋Š” case๋ฅผ ์ฐพ์•„ ๋ณ€๊ฒฝ๋œ state๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  4. ๊ฐ๊ฐ์˜ case์—์„œ ๋ฆฌํ„ด๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ณด๋ฉด ๊ธฐ์กด state๋ฅผ spread ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ด๋Š” reducerํ•จ์ˆ˜์—์„œ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ๋งŒ๋“ค ๋•Œ์—๋Š” ๋ถˆ๋ณ€์„ฑ์„ ์ง€์ผœ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

  5. ์ถ”๊ฐ€๋กœ default๋ฅผ ์„ค์ •ํ•˜์—ฌ case์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” aciton.type์ด ๋“ค์–ด์™”์„ ๋•Œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ์žˆ๋‹ค.

reducerํ•จ์ˆ˜์—์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ๊ธฐ์กด์˜ state๋ฅผ ์ƒˆ๋กœ์šด state๋กœ ๋Œ€์ฒด(replace)๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ๊ธฐ์กด์˜ state๋ฅผ ๋ณ€๊ฒฝ(modify)ํ•˜๊ฑฐ๋‚˜, ์ถ”๊ฐ€(add)ํ•˜๊ฑฐ๋‚˜, ๋ฎ์–ด์“ฐ์ง€(overwrite) ์•Š์•„์•ผ ํ•œ๋‹ค.


8. Conclusion

์‚ฌ์‹ค ์œ„์˜ ์˜ˆ์ œ๋Š” useReducer()ํœต์šธ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค useState()ํ›…์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ ์ ์„ ์ฐธ๊ณ ํ•ด์ฃผ์—ˆ์œผ๋ฉด ํ•œ๋‹ค. useReducer()ํ›…์€ ์—ฌ๋Ÿฌ ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋˜ ํ›…์ด๋‹ค. ํ•˜์ง€๋งŒ ์˜ˆ์ „์— redux๋ฅผ ๋ฐฐ์šธ ๋•Œ ๋น„์Šทํ•œ ๊ฐœ๋…์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ–ˆ๋˜ ๊ธฐ์–ต์ด ๋– ์˜ฌ๋ผ ์‰ฝ๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜์—ˆ๋˜ ๊ฑฐ ๊ฐ™๋‹ค. ๋‚˜์ค‘์— redux๋ฅผ ๋‹ค์‹œ ๊ณต๋ถ€ํ•  ๋•Œ ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ์ด ํฐ ๋„์›€์ด ๋˜์—ˆ์Œ ํ•œ๋‹ค.


์ฐธ๊ณ 

20. useReducer ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ ์—…๋ฐ์ดํŠธ ๋กœ์ง ๋ถ„๋ฆฌํ•˜๊ธฐ React Hooks :: useReducer์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ


๐Ÿ‘†

๐Ÿ“… 2022-08-09

Last updated