Thinking in React

ν•™μŠ΅ λ‚΄μš©

λ¦¬μ•‘νŠΈ 곡식 λ¬Έμ„œμ— μ†Œκ°œλœ πŸ”— Thinking in React 예제λ₯Ό ν†΅ν•œ μ½”λ“œ μ‹€μŠ΅

Thinking in React

1. step 0 Start with the mockup

  • 개발 ν™˜κ²½ μ„ΈνŒ… (vite)

     npm create vite@latest 
  • Thinking in React 예제의 μ „μ œ 쑰건 및 Mockup νŒŒμ•…

    • Mockup κ³Ό JSON ν˜•μ‹μ˜ API 확인 ν›„ β‡’ μ‚¬μš©μžκ°€ λ³Ό 수 μžˆλ„λ‘ UI 섀계

UI

2. step 1 Build a static version in React

  1. Static Version UI κ΅¬ν˜„ (퍼블리싱 μž‘μ—…)

  2. JSON μ—μ„œ μΉ΄ν…Œκ³ λ¦¬μ˜ value 값을 μΆ”μΆœ β‡’ reduce λ©”μ„œλ“œλ₯Ό 톡해

  [
    { category: "Fruits", price: "$1", stocked: true, name: "Apple" },
    { category: "Fruits", price: "$1", stocked: true, name: "Dragonfruit" },
    { category: "Fruits", price: "$2", stocked: false, name: "Passionfruit" },
    { category: "Vegetables", price: "$2", stocked: true, name: "Spinach" },
    { category: "Vegetables", price: "$4", stocked: false, name: "Pumpkin" },
    { category: "Vegetables", price: "$1", stocked: true, name: "Peas" }
  ]
  const categories = products.reduce(
    (acc: string[], product: Product) =>
      acc.includes(product.category) ? acc : [...acc, product.category],
    []
  );

3. step 1 Break the UI into a component hierarchy

  • UIλ₯Ό ꡬ성 μš”μ†Œ 계측 ꡬ쑰둜 λ‚˜λˆ„κΈ° (μ»΄ν¬λ„ŒνŠΈ ꡬ성)

  1. JSON API 기반으둜 UI κ΅¬ν˜„ν•˜λ‹ˆ, 반볡적인 μš”μ†Œκ°€ 보여지고 μ½”λ“œκ°€ 길어짐 β‡’ table tbody μ˜μ—­ μ»΄ν¬λ„ŒνŠΈλ‘œ 뢄리 ProductsInCategory

  2. ProductInCategoryμ»΄ν¬λ„ŒνŠΈλ₯Ό λΆ„λ¦¬ν•˜κ³  λ³΄λ‹ˆ, 반볡적인 μš”μ†Œ 발견 β‡’ ProductRow ProductCategoryRow μ»΄ν¬λ„ŒνŠΈ 뢄리

  3. ProductTable μ»΄ν¬λ„ŒνŠΈλ₯Ό μƒμ„±ν•΄μ„œ table μ˜μ—­ 전체 λ§΅ν•‘

  4. 검색 μ˜μ—­ SearchBar μ»΄ν¬λ„ŒνŠΈλ‘œ 뢄리

  5. FilterableProductTable table κ³Ό 검색 μ˜μ—­ 전체 λ§΅ν•‘

  6. SearchBar λΆ„λ¦¬ν•˜κ³  μ²΄ν¬λ°•μŠ€ field CheckBoxField μ»΄ν¬λ„ŒνŠΈ 뢄리

  7. SearchBar μ»΄ν¬λ„ŒνŠΈμ—μ„œ μΆ”κ°€λ‘œ input field TextField μ»΄ν¬λ„ŒνŠΈ 뢄리

  • 5.FilterableProductTable

    • 4.SearchBar

      • 7.TextField

      • 6.CheckBoxField

    • 3.ProductTable

      • 1.ProductsInCategory

        • 2.ProductCategoryRow

        • 2.ProductRow

Component

4. step 3 Find the minimal but complete representation of UI state

  • state(μƒνƒœ)κ°€ ν•„μš”ν•œ 뢀뢄을 식별

    • μ›λž˜ μ œν’ˆ λͺ©λ‘ (props)

    • μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 검색어 (state) TextField

    • μ²΄ν¬λ°•μŠ€μ˜ κ°’ (state) CheckBoxField

    • ν•„ν„°λ§λœ μ œν’ˆ λͺ©λ‘ (props)

5. step 4 Identify where your state should live

  • state(μƒνƒœ)λ₯Ό 식별 ν•œ ν›„ state(μƒνƒœ)의 곡톡 μƒμœ„ μš”μ†Œλ₯Ό μ°Ύμ•„ state(μƒνƒœ)의 μœ„μΉ˜ κ²°μ •

    • state(μƒνƒœ)κ°€ 곡유 λ˜μ–΄μ•Ό ν•˜λŠ” 곡톡 μƒμœ„ μ»΄ν¬λ„ŒνŠΈ FilterableProductTable

6. step 5 Add inverse data flow

  • state(μƒνƒœ)λ₯Ό μ—…λ°μ΄νŠΈ ν•˜κΈ° μœ„ν•œ μ½œλ°±ν•¨μˆ˜λ₯Ό μ—­λ°©ν–₯으둜 λŒμ–΄μ˜¬λ¦¬κΈ°

    • μ‚¬μš©μžκ°€ μž…λ ₯ν•œ 검색어 (state) TextField

    • μ²΄ν¬λ°•μŠ€μ˜ κ°’ (state) CheckBoxField

    μ»΄ν¬λ„ŒνŠΈμ—μ„œ useState둜 μƒμ„±ν•œ state λŒμ–΄μ˜¬λ € 곡유 μ»΄ν¬λ„ŒνŠΈμ— μœ„μΉ˜ μ‹œν‚€κ³ , propsλ₯Ό 톡해 ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈμ—κ²Œ state와 state λ³€κ²½ν•˜κΈ° μœ„ν•¨ ν•¨μˆ˜λ₯Ό 전달

Last updated