useContext()

1. ๊ฐœ์š”

useContext()ํ›…์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” React์—์„œ์˜ Context๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.

Context์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ณ  useContext()์˜ ์‚ฌ์šฉ๊ณผ ํŠน์ง•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.


2. Context

์ผ๋ฐ˜์ ์ธ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ (์ฆ‰, ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ž์‹์—๊ฒŒ) props๋ฅผ ํ†ตํ•ด ์ „๋ ๋˜์ง€๋งŒ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•ˆ์˜ ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋“ค์— ์ „ํ•ด์ค˜์•ผ ํ•˜๋Š” props์˜ ๊ฒฝ์šฐ ์ด ๊ณผ์ •์ด ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. context๋ฅผ ์ด์šฉํ•˜๋ฉด, ํŠธ๋ฆฌ ๋‹จ๊ณ„๋งˆ๋‹ค ๋ช…์‹œ์ ์œผ๋กœ props๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์•„๋„ ๋งŽ์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด๋Ÿฌํ•œ ๊ฐ’์„ ๊ณต์œ ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„๋Š” ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ์— ๋‚˜ํƒ€๋‚œ Context์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค.

์ฆ‰, ๊ฐ™์€ props๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋งŽ์ด ์ „๋‹ฌํ•˜์—ฌ ๋งŽ์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ณต์œ ํ•˜๊ณ  ์žˆ๋‹จ๋ฉด Context๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๋žตํ•˜๊ณ  ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•œ ๋ฒˆ ์ „์—ญ์œผ๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.


2-1. Context API - React.createContext

const MyContext = React.createContext(defaultValue);

Context ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” API์ด๋‹ค. Context ๊ฐ์ฒด๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•  ๋•Œ React๋Š” ํŠธ๋ฆฌ ์ƒ์œ„์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์ด ์žˆ๋Š” ์ง์ด ๋งž๋Š” Provider๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ๊ฐ’์„ ์ฝ๋Š”๋‹ค.


2-2. Context API - Context.Provider

<MyContext.Provider value={/* ์–ด๋–ค ๊ฐ’ */}>

Context ์˜ค๋ธŒ์ ํŠธ์— ํฌํ•จ๋œ React ์ปดํฌ๋„ŒํŠธ์ธ Provider๋Š” context๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ context์˜ ๋ณ€ํ™”๋ฅผ ์•Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

Provider ์ปดํฌ๋„ŒํŠธ๋Š” value props์„ ๋ฐ›์•„์„œ ์ด ๊ฐ’์„ ํ•˜์œ„์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค. ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ˆ˜์— ์ œํ•œ์€ ์—†๋‹ค. ๋งŒ์•ฝ Provider ์ปดํฌ๋„ŒํŠธ ํ•˜์œ„์— ๋˜ ๋‹ค๋ฅธ Provider ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ํ•˜์œ„ Provider์˜ ๊ฐ’์ด ์šฐ์„ ์‹œ ๋œ๋‹ค.

Provider ์ปดํฌ๋„ŒํŠธ์˜ value props๊ฐ€ ๋ฐ”๋€” ๋•Œ ๋งˆ๋‹ค ํ•ด๋‹น Provider์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค.


2-3. Context API - Context.Consumer

<MyContext.Consumer>
    {value => /* context ๊ฐ’์„ ์ด์šฉํ•œ ๋ Œ๋”๋ง */}
</MyContext.Consumer>

context ๋ณ€ํ™”๋ฅผ ๊ตฌ๋…ํ•˜๋Š” React ์ปดํฌ๋„ŒํŠธ์ด๋‹ค. ์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์•ˆ์—์„œ context๋ฅผ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋‹ค.

Context.Consumer ์ปดํฌ๋„ŒํŠธ์˜ ์ž์‹์€ ํ•จ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” context์˜ ํ˜„์žฌ๊ฐ’์„ ๋ฐ›๊ณ  React๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๊ฐ€ ๋ฐ›๋Š” value ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์€ ํ•ด๋‹น context์˜ Provider ์ปดํฌ๋„ŒํŠธ ์ค‘ ์ƒ์œ„ ํŠธ๋ฆฌ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด Provider ์ปดํฌ๋„ŒํŠธ์˜ value props๊ณผ ๋™์ผํ•˜๋‹ค.


3. Context๋ฅผ ์‚ฌ์šฉํ•œ ์ œ์ฃผ๋„ ๊ธฐ๋…ํ’ˆ ์ปดํฌ๋„ŒํŠธ

์ œ์ฃผ๋„ ์—ฌํ–‰ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก์„ ๋งŒ๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ Context๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ƒˆ๋กญ๊ฒŒ ์ž‘์„ฑํ•ด๋ณด์ž.

//App.js
import { useState, createContext } from "react";
import Children from "./Children";


// 1) createContext๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Context ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ
export const AppContext = createContext({
  gift: ["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"],
  addGift: () => {},
});

function App() {
  const [gift, setGift] = useState(["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"]);

  const addGift = (gift) => {
    setGift((prev) => [...prev, gift]);
  };
  return (
    // 2) AppContext.Provider ์ƒ์„ฑํ•˜์—ฌ Context์˜ ๋ณ€ํ™” ์•Œ๋ฆฌ๊ธฐ
    <AppContext.Provider value={{ gift, addGift }}>
      <div>์•ˆ๋…•, ์ œ์ฃผ!</div>
      <Children />
    </AppContext.Provider>
  );
}

export default App;

// Children.js
import React from "react";
import { useForm } from "react-hook-form";
import { AppContext } from "./App";

const Children = () => {
  const { register, handleSubmit, setValue, getValues } = useForm();

  return (
    // 3) AppContext.Consumer๋ฅผ ํ†ตํ•ด Context๋ฅผ ๊ตฌ๋…ํ•˜๊ธฐ
    <AppContext.Consumer>
      {({ gift, addGift }) => (
        <div>
          <form
            onSubmit={handleSubmit(() => {
              console.log(`${gift}๊ฐ€ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค๐Ÿ˜€`);
              addGift(getValues("gift"));
              setValue("gift", "");
            })}
          >
            <input {...register("gift")} placeholder="์‚ฌ๊ณ  ์‹ถ์€ ๊ธฐ๋…ํ’ˆ" />
            <input type="submit" value="๊ธฐ๋…ํ’ˆ ๊ณ ๋ฅด๊ธฐ" />
          </form>
          <div>๋‚˜์˜ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก: {gift.join(", ")}</div>
        </div>
      )}
    </AppContext.Consumer>
  );
};

export default Children;

1) createContext๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Context ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

export const AppContext = createContext({
  gift: ["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"],
  addGift: () => {},
});

createcontext API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Context ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋‹ค. defaultValue๋กœ๋Š” ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐ›๊ณ  ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜(Provider API์˜ value) ๋ชจ์–‘๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.


2) AppContext.Provider ์ƒ์„ฑํ•˜์—ฌ Context์˜ ๋ณ€ํ™” ์•Œ๋ฆฌ๊ธฐ

<AppContext.Provider value={{ gift, addGift }}>
  <div>์•ˆ๋…•, ์ œ์ฃผ!</div>
  <Children />
</AppContext.Provider>

gift๋Š” App์ปดํฌ๋„ŒํŠธ์—์„œ ๋งŒ๋“  state๊ฐ’์ด๊ณ  addGiftํ•จ์ˆ˜๋Š” gift์˜ ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด๋ฅผ Proivder API์˜ value๋ฅผ ํ†ตํ•ด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•ด๋‹น ๊ฐ’๋“ค์„ ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

์ฒ˜์Œ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋˜ ๋ถ€๋ถ„์ด๋‹ค. value props์˜ ๊ฐ’์ด Object์ด๋ฉด AppContext.Consumer์ปดํฌ๋„ŒํŠธ์—์„œ Object๋กœ ๋ฐ›๊ณ  Array์ด๋ฉด AppContext.Consumer์ปดํฌ๋„ŒํŠธ์—์„œ Array๋กœ ๋ฐ›์ž


3) AppContext.Consumer๋ฅผ ํ†ตํ•ด Context๋ฅผ ๊ตฌ๋…ํ•˜๊ธฐ

<AppContext.Consumer>
    {({ gift, addGift }) => ()}
</AppContext.Consumer>

consumer API๋ฅผ ํ†ตํ•ด AppContext.Consumer์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ ์ž์‹์€ ํ•จ์ˆ˜๋กœ ์ธ์ž๋กœ AppContext.Consumer์ปดํฌ๋„ŒํŠธ์—์„œ ์ „๋‹ฌํ•œ value props์ด ๋“ค์–ด์žˆ๋‹ค. ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ํ™”๋ฉด์„ ๊ทธ๋ฆฌ๋ฉด ๋œ๋‹ค.

ํ™•์‹คํžˆ props๊ฐ€ ํ•„์š”์—†๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ ๊นŒ์ง€ props๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค Context๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”ฑ ํ•„์š”ํ•œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ๋งŒ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ๊น”๋”ํ•ด ๋ณด์ธ๋‹ค. ์ด์ œ ์•„๋ž˜์—์„œ Context๋ฅผ ๋” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” useContext()ํ›…์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž.


4. useContext()ํ›… ์‚ฌ์šฉํ•˜๊ธฐ

useContext()์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

const value = useContext(MyContext);

context ๊ฐ์ฒด(React.createContext์—์„œ ๋ฐ˜ํ™˜๋œ ๊ฐ’)์„ ๋ฐ›์•„ ๊ทธ context์˜ ํ˜„์žฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. context์˜ ํ˜„์žฌ ๊ฐ’์€ ํŠธ๋ฆฌ ์•ˆ์—์„œ ์ด Hook๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ๊ฐ€์žฅ ๊ฐ€๊นŒ์ด์— ์žˆ๋Š” <MyContext.Provider>์˜ value props์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค.

useContext()ํ›…์„ ํ˜ธ์ถœํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” context๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ํ•ญ์ƒ ๋ฆฌ๋ Œ๋”๋ง ๋œ๋‹ค.

์ด์ œ useContext()ํ›…์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ Children.js ์ปดํฌ๋„ŒํŠธ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์ž.

// Children.js
import React, { useContext } from "react";
import { useForm } from "react-hook-form";
import { AppContext } from "./App";

const Children = () => {
  const { gift, addGift } = useContext(AppContext);
  const { register, handleSubmit, setValue, getValues } = useForm();

  return (
    <div>
      <form
        onSubmit={handleSubmit(() => {
          console.log(`${getValues("gift")}๊ฐ€ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค๐Ÿ˜€`);
          addGift(getValues("gift"));
          setValue("gift", "");
        })}
      >
        <input {...register("gift")} placeholder="์‚ฌ๊ณ  ์‹ถ์€ ๊ธฐ๋…ํ’ˆ" />
        <input type="submit" value="๊ธฐ๋…ํ’ˆ ๊ณ ๋ฅด๊ธฐ" />
      </form>
      <div>๋‚˜์˜ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก: {gift.join(", ")}</div>
    </div>
  );
};

export default Children;

<AppContext.Consumer>์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—†์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‹ ์— useContext()ํ›…์„ ํ†ตํ•ด gift์™€ addGift๋ฅผ ๋ถˆ๋Ÿฌ์™€ AppContext์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

useContext()ํ›…์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ด์ „ ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.


5. Conclusion

useContext()ํ›…๋„ useReducer()ํ›…๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ ๋ฒˆ๋„ ์‚ฌ์šฉํ•ด๋ณธ ์ ์ด ์—†๋Š” React Hook์ด์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฒ˜์Œ์—๋Š” ์ƒ์†Œํ•˜์˜€์ง€๋งŒ ์ƒ๊ฐ๋ณด๋‹ค ๊ทธ๋ฆฌ? ์–ด๋ ต๊ณ  ๋ณต์žกํ•œ ๊ฐœ๋…์ด ์•„๋‹ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธˆ๋ฐฉ ์ดํ•ด๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค. ๊ถ๊ธˆํ•œ ์ ์€ useContext()ํ›…๊ณผ useReducer()ํ›…์ด porps๋ฅผ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฑด๋„ˆ ๋›ฐ๊ณ  ํ•„์š”ํ•œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ๋งŒ props๋ฅผ ์ „๋‹ฌ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํ›…์œผ๋กœ ๋ณด์ธ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋‘๊ฐ€์ง€์˜ ํ›…์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€์ง€ ์•„๋‹ˆ๋ฉด ์ƒํ™ฉ์— ๋”ฐ๋ผ ํ•œ ๊ฐ€์ง€๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€์ง€๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค.

๋งˆ๋ฌด๋ฆฌ๋กœ useContext()ํ›…๊ณผ useReducer()ํ›…์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค. ํ™•์‹คํžˆ ์ปดํฌ๋„ŒํŠธ์˜ ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•ด์ง„๋‹ค๋ฉด ์ด ํ›…๋“ค์€ ์œ ์šฉํ•ด์งˆ ๊ฒƒ ๊ฐ™๋‹ค๐Ÿ˜ƒ ๊ทธ๋ฆฌ๊ณ  ๊ฐ™์€ ๋‚ด์šฉ์ด์ง€๋งŒ ์ฝ”๋“œ์— ์‚ฌ์šฉ๋œ ํ›…์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ๋„ ์žฌ๋ฐŒ๋Š” ๋ถ€๋ถ„์ด๋‹ค.(์‚ฌ์‹ค ์ด๊ฑด ๊ทธ๋ƒฅ useState๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€๊ฑฐ ๊ฐ™๋‹ค๐Ÿ˜‚)

// App.js
import { createContext, useReducer } from "react";
import Children from "./Children";

const inItGift = ["ํ•œ๋ผ๋ด‰", "์ดˆ์ฝœ๋ฆฟ"];

const reducer = (gift, action) => {
  switch (action.type) {
    case "submitGift":
      return [...gift, action.gift];
    default:
      throw new Error("type Error");
  }
};

export const AppContext = createContext({
  inItGift,
  dispatch: () => {},
});

function App() {
  const [gift, dispatch] = useReducer(reducer, inItGift);
  return (
    <AppContext.Provider value={{ gift, dispatch }}>
      <div>์•ˆ๋…•, ์ œ์ฃผ!</div>
      <Children />
    </AppContext.Provider>
  );
}

export default App;

// Children.js
import React, { useContext } from "react";
import { useForm } from "react-hook-form";
import { AppContext } from "./App";

const Children = () => {
  const { gift, dispatch } = useContext(AppContext);
  const { register, handleSubmit, setValue } = useForm();

  const onSubmitFrom = (data) => {
    const { gift } = data;
    console.log(`${gift}๊ฐ€ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค๐Ÿ˜€`);
    dispatch({
      type: "submitGift",
      gift,
    });
    setValue("gift", "");
  };

  return (
    <div>
      <form onSubmit={handleSubmit(onSubmitFrom)}>
        <input {...register("gift")} placeholder="์‚ฌ๊ณ  ์‹ถ์€ ๊ธฐ๋…ํ’ˆ" />
        <input type="submit" value="๊ธฐ๋…ํ’ˆ ๊ณ ๋ฅด๊ธฐ" />
      </form>
      <div>๋‚˜์˜ ๊ธฐ๋…ํ’ˆ ๋ชฉ๋ก: {gift.join(", ")}</div>
    </div>
  );
};

export default Children;

์ฐธ๊ณ 

[TIL #6] React (Hooks) - useContext ๋ž€? Context


๐Ÿ‘†

๐Ÿ“… 2022-08-10

Last updated