Closed SMJminjeong closed 3 months ago
Context APIλ νμ μ»΄ν¬λνΈλ‘ Props κ°μ μ λ¬ν νμ μμ΄ νμ μ»΄ν¬λνΈμμ μ΅μμ μ»΄ν¬λνΈκ° μμ ν κ°κ³Ό κ°±μ ν¨μμ μ§μ μ κ·Όν μ μλ μ₯μ μ κ°μ§κ³ μμ΅λλ€.
import { createContext } from "react";
// Toastμ μνλ₯Ό μμ ν Context
export const ToastStateContext = createContext(initailState);
μ μ μνλ₯Ό κ΄λ¦¬ν μ μμ΄, propsλ₯Ό ν΅ν΄ μΌμΌμ΄ λ°μ΄ν°λ₯Ό μ λ¬νμ§ μκ³ λ μ¬λ¬ μ»΄ν¬λνΈμμ μ½κ² μ κ·Όν μ μμ΅λλ€.
import React, { createContext } from 'react';
// EmailContext μμ±
export const EmailContext = createContext();
Propsλ‘ μ λ¬ν νμ μμ΄ νμ μ»΄ν¬λνΈμμ μ΅μμ μ»΄ν¬λνΈκ° μμ ν κ°κ³Ό κ°±μ ν¨μμ μ§μ μ κ·Όμ΄ κ°λ₯ν©λλ€. μ΄μ λ°λΌ Prop Drilling λ°©μ§μ μ½λ κ°λ μ±μ΄ ν₯μλ©λλ€.
import { createContext } from "react";
// Toastμ μν κ°±μ ν¨μλ₯Ό μμ ν Context
export const ToastActionContext = createContext(initialAction);
Prop Drilling λ°©μ§ μ κ°λ μ± ν₯μ
import { createContext } from "react";
// Toastμ μνλ₯Ό μμ ν Context
export const ToastStateContext = createContext(initailState);
π p.137 β ContextAPI λ₯Ό μ¬μ©νλ©΄ μ’μ μ κ³Ό ν΄λΉ Context μ μνλ₯Ό μμ νλ ν¨μ μ½λλ₯Ό μμ±ν΄μ£ΌμΈμ.