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