TIL : route vs components

RamiΒ·2026λ…„ 1μ›” 5일

TodayILearn

λͺ©λ‘ 보기
58/66

ν•œ 쀄 μš”μ•½

  • routes πŸ‘‰ β€œURL에 직접 λŒ€μ‘λ˜λŠ” νŽ˜μ΄μ§€ λ‹¨μœ„ μ»΄ν¬λ„ŒνŠΈβ€
  • components πŸ‘‰ β€œνŽ˜μ΄μ§€λ₯Ό κ΅¬μ„±ν•˜λŠ” μž¬μ‚¬μš© κ°€λŠ₯ν•œ 쑰각듀”

1️⃣ routes 폴더: β€œνŽ˜μ΄μ§€β€λΌκ³  μ΄ν•΄ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€

routes/
 β”œβ”€ home.tsx
 β”œβ”€ login.tsx
 β”œβ”€ create-account.tsx
 └─ profile.tsx

이 νŒŒμΌλ“€μ˜ 곡톡점은 보톡 이런 ν˜•νƒœμž…λ‹ˆλ‹€:

// routes/login.tsx
export default function Login() {
  return (
    <div>
      <h1>둜그인</h1>
      {/* μ—¬λŸ¬ μ»΄ν¬λ„ŒνŠΈ μ‘°ν•© */}
    </div>
  );
}

routes의 νŠΉμ§•

  • URLκ³Ό 1:1 λŒ€μ‘

    • /login β†’ login.tsx
    • /profile β†’ profile.tsx
  • 보톡 λΌμš°ν„°μ—μ„œ 직접 import됨

  • ν•œ ν™”λ©΄ 전체λ₯Ό μ±…μž„μ§

  • λ‹€λ₯Έ νŽ˜μ΄μ§€μ—μ„œ μž¬μ‚¬μš©λ˜λŠ” κ²½μš°λŠ” 거의 μ—†μŒ

πŸ‘‰ κ·Έλž˜μ„œ
βœ… β€œλΌμš°νŠΈ = 각각의 νŽ˜μ΄μ§€β€
라고 μ΄ν•΄ν•˜μ…”λ„ λ©λ‹ˆλ‹€.


2️⃣ components 폴더: β€œμž¬λ£Œ / λΆ€ν’ˆ / 블둝”

components/
 └─ layout.tsx

components의 νŠΉμ§•

  • URLκ³Ό 직접적인 관계 μ—†μŒ
  • μ—¬λŸ¬ νŽ˜μ΄μ§€μ—μ„œ κ³΅ν†΅μœΌλ‘œ μ‚¬μš©
  • UI 쑰각 / 둜직 λ‹¨μœ„

예λ₯Ό λ“€λ©΄:

  • Header
  • Footer
  • Layout
  • Button
  • Input
  • Modal
  • Avatar
  • PostCard
// components/layout.tsx
export default function Layout({ children }) {
  return (
    <>
      <Header />
      <main>{children}</main>
      <Footer />
    </>
  );
}

πŸ‘‰ 이 Layout은

  • homeμ—μ„œλ„
  • profileμ—μ„œλ„
  • loginμ—μ„œλ„
    ν•„μš”ν•˜λ©΄ μ–΄λ””λ“  μ‚¬μš© κ°€λŠ₯

κ·Έλž˜μ„œ λ§μ”€ν•˜μ‹  ν‘œν˜„ 쀑 이 뢀뢄이 μ •ν™•ν•©λ‹ˆλ‹€ πŸ‘‡

νŽ˜μ΄μ§€μ— μ‚½μž…λ˜λŠ” κ΅¬μ„±μš”μ†Œ
μ–΄λ””μ„œλ“  μ›ν•˜λ©΄ νŽ˜μ΄μ§€ μ–΄λ””λ“  μΆ”κ°€ν•  수 μžˆλŠ” μš”μ†Œ

βœ”οΈ λ§žμŠ΅λ‹ˆλ‹€.


3️⃣ App.tsx / main.tsxκ°€ ν•˜λŠ” μ—­ν•  (μ€‘μš”)

보톡 κ΅¬μ‘°λŠ” μ΄λ ‡κ²Œ ν˜λŸ¬κ°‘λ‹ˆλ‹€:

main.tsx
 └─ App.tsx
     └─ Router
         └─ routes/*
// App.tsx μ˜ˆμ‹œ
<BrowserRouter>
  <Layout>
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/login" element={<Login />} />
    </Routes>
  </Layout>
</BrowserRouter>

μ—¬κΈ°μ„œ 핡심:

  • routes β†’ Router에 λ“±λ‘λ˜λŠ” λŒ€μƒ
  • components β†’ routes μ•ˆμ—μ„œ μ‘°ν•©λ˜λŠ” λŒ€μƒ

4️⃣ λΉ„μœ λ‘œ ν•œ λ²ˆμ— 정리

🏠 μ›Ήμ‚¬μ΄νŠΈ = 건물

  • routes = 각 λ°©

    • κ±°μ‹€(Home)
    • μΉ¨μ‹€(Profile)
    • ν˜„κ΄€(Login)
  • components = 가ꡬ

    • μ†ŒνŒŒ(Header)
    • ν…Œμ΄λΈ”(Button)
    • μ‘°λͺ…(Layout)

πŸ‘‰ 방은 μ£Όμ†Œ(URL)κ°€ μžˆμ§€λ§Œ
πŸ‘‰ κ°€κ΅¬λŠ” μ£Όμ†Œκ°€ μ—†μŠ΅λ‹ˆλ‹€


5️⃣ 싀무 κΈ°μ€€μœΌλ‘œ 폴더λ₯Ό λ‚˜λˆ„λŠ” νŒλ‹¨ κΈ°μ€€

❓ 이 νŒŒμΌμ€ 어디에 둬야 ν• κΉŒ?

μ•„λž˜ μ§ˆλ¬Έμ„ ν•΄λ³΄μ„Έμš”

  1. URL둜 직접 μ ‘κ·Όν•΄μ•Ό ν•˜λ‚˜?

    • YES β†’ routes
  2. λ‹€λ₯Έ νŽ˜μ΄μ§€μ—μ„œλ„ μž¬μ‚¬μš©λ  κ°€λŠ₯성이 μžˆλ‚˜?

    • YES β†’ components
  3. Router에 λ“±λ‘λ κΉŒ?

    • YES β†’ routes

μ§€κΈˆ κ΅¬μ‘°λŠ” 쒋은가?

βœ… λ„€, μ§€κΈˆ κ΅¬μ‘°λŠ” μ•„μ£Ό κ΅κ³Όμ„œμ μΈ 초기 κ΅¬μ‘°μž…λ‹ˆλ‹€.
특히 layout.tsxλ₯Ό components에 λ‘” 것도 μ •ν™•ν•©λ‹ˆλ‹€.

λ‚˜μ€‘μ— ν”„λ‘œμ νŠΈ 컀지면 보톡 μ΄λ ‡κ²Œ ν™•μž₯λ©λ‹ˆλ‹€:

components/
 β”œβ”€ layout/
 β”œβ”€ ui/
 β”œβ”€ auth/
 └─ common/

routes/
 β”œβ”€ home.tsx
 β”œβ”€ login.tsx
 └─ profile.tsx

λ§ˆμ§€λ§‰ 정리 (μ§„μ§œ 핡심)

  • routes = νŽ˜μ΄μ§€
  • components = νŽ˜μ΄μ§€λ₯Ό λ§Œλ“œλŠ” λΆ€ν’ˆ
  • routesλŠ” componentsλ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ
  • componentsλŠ” routesλ₯Ό λͺ¨λ¦„
profile
YOLO

0개의 λŒ“κΈ€