super-ealry-bird / TESTING_FRONTEND

2 stars 0 forks source link

7.2 React Context λž€? #37

Closed SMJminjeong closed 3 months ago

SMJminjeong commented 3 months ago

πŸ“ p.137 ❓ ContextAPI λ₯Ό μ‚¬μš©ν•˜λ©΄ 쒋은 점과 ν•΄λ‹Ή Context 의 μƒνƒœλ₯Ό μ†Œμœ ν•˜λŠ” ν•¨μˆ˜ μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ£Όμ„Έμš”.

export const initialState: ToastState = {
  isShown: false,
  message: "",
  style: "succeed",
};
sbkimm commented 3 months ago

Context APIλŠ” ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈλ‘œ Props 값을 전달할 ν•„μš” 없이 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ΅œμƒμœ„ μ»΄ν¬λ„ŒνŠΈκ°€ μ†Œμœ ν•œ κ°’κ³Ό κ°±μ‹  ν•¨μˆ˜μ— 직접 μ ‘κ·Όν•  수 μžˆλŠ” μž₯점을 가지고 μžˆμŠ΅λ‹ˆλ‹€.

import { createContext } from "react";

// Toast의 μƒνƒœλ₯Ό μ†Œμœ ν•  Context
export const ToastStateContext = createContext(initailState);
yjkim-89 commented 3 months ago

μ „μ—­ μƒνƒœλ₯Ό 관리할 수 μžˆμ–΄, propsλ₯Ό 톡해 일일이 데이터λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šκ³ λ„ μ—¬λŸ¬ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ‰½κ²Œ μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

import React, { createContext } from 'react';

// EmailContext 생성
export const EmailContext = createContext();
kkite6789 commented 3 months ago

Props둜 전달할 ν•„μš” 없이 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ΅œμƒμœ„ μ»΄ν¬λ„ŒνŠΈκ°€ μ†Œμœ ν•œ κ°’κ³Ό κ°±μ‹  ν•¨μˆ˜μ— 직접 접근이 κ°€λŠ₯ν•©λ‹ˆλ‹€. 이에 따라 Prop Drilling 방지와 μ½”λ“œ 가독성이 ν–₯μƒλ©λ‹ˆλ‹€.

import { createContext } from "react";

// Toast의 μƒνƒœ κ°±μ‹  ν•¨μˆ˜λ₯Ό μ†Œμœ ν•  Context
export const ToastActionContext = createContext(initialAction);
SMJminjeong commented 3 months ago

Prop Drilling 방지 와 가독성 ν–₯상

import { createContext } from "react";

// Toast의 μƒνƒœλ₯Ό μ†Œμœ ν•  Context
export const ToastStateContext = createContext(initailState);