[React] React Router

Hannahhhยท2022๋…„ 8์›” 1์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
7/30

๐Ÿ” React Router


๐Ÿ‘€ SPA & Routing


SPA๋Š” ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ํ™”๋ฉด์„ ์‚ฌ์šฉํ•œ๋‹ค.


์•„๋ž˜๋Š” Twittler์˜ ์˜ˆ์‹œ์ด๋ฉฐ, ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋‚˜ ๋ฉ”์ธ ํŠธ์œ— ๋ชจ์Œ ํŽ˜์ด์ง€, ์•Œ๋ฆผ ํŽ˜์ด์ง€, ๋งˆ์ด ํŠธ์œ— ํŽ˜์ด์ง€ ๋“ฑ์˜ ํ™”๋ฉด์„ ํ•„์š”๋กœ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.


๊ฐ ํ™”๋ฉด์— ๋”ฐ๋ผ ์ฃผ์†Œ๋„ ๋‹ฌ๋ผ์ง€๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ๋‹ค๋ฅธ ์ฃผ์†Œ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ณผ์ •์„ Routing์ด๋ผ๊ณ  ํ•œ๋‹ค. ( Routing: ๊ฒฝ๋กœ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝํ•œ๋‹ค. )

๊ทธ๋Ÿฌ๋‚˜, React ์ž์ฒด์— ๋ผ์šฐํŒ… ๊ธฐ๋Šฅ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์ฃผ์†Œ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค˜์•ผํ•œ๋‹ค.

์ด ๋•Œ, React Router ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.




โœ” React Router


React Router์˜ ์ฃผ์š” ์ปดํฌ๋„ŒํŠธ๋Š” ํฌ๊ฒŒ 3๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค.


์œ„์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” React Router ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์™€์•ผํ•˜๋Š”๋ฐ, ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์จ์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

import { BrowserRouter, Routes, Route, Link } from "react-router-dom"



โš™ React Router ์‚ฌ์šฉ ํ™˜๊ฒฝ ์…‹ํŒ…


1) React router ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

ํ„ฐ๋ฏธ๋„์— npx create-react-app <ํ”„๋กœ์ ํŠธ ํด๋” ๋ช…> ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด React ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ ๋งŒ๋“  ํ›„,

npm install react-router-dom@^6.3.0 ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด React-router ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ๋‹ค.


2) App.js๋กœ React-router ์ปดํฌ๋„ŒํŠธ ๊บผ๋‚ด์˜ค๊ธฐ

App.js ํŒŒ์ผ ์ตœ์ƒ๋‹จ์— import { BrowserRouter, Routes, Route, Link } from "react-router-dom" ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด react-router ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.




๐Ÿ‘€ React-router ์‚ฌ์šฉ ์˜ˆ์‹œ


1 ) React router ์‚ฌ์šฉ ํ™˜๊ฒฝ ์…‹ํŒ… ํ›„, package.jsonํŒŒ์ผ์˜ dependencies ํ•ญ๋ชฉ์— react-router-dom ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋“ฑ๋ก ๋๋Š”์ง€ ํ™•์ธ.


2 ) App.js ์ตœํ•˜๋‹จ์— ํŽ˜์ด์ง€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ


3 ) ๊ฐ ์ปดํฌ๋„ŒํŠธ๋กœ ์ด๋™ํ•  ๋ฉ”๋‰ด ์ œ์ž‘: <ul>, <li> ์š”์†Œ ์ด์šฉ


4 ) ์ฃผ์†Œ์— ๋”ฐ๋ผ ํŽ˜์ด์ง€ ๋ทฐ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ค๊ธฐ

Home ํŽ˜์ด์ง€์˜ ์ฃผ์†Œ "/"
MyPage ํŽ˜์ด์ง€์˜ ์ฃผ์†Œ "/mypage"
Dashboard ํŽ˜์ด์ง€์˜ ์ฃผ์†Œ "/dashboard"


โœ” BrowerRouter

<BrowserRouter> ์ปดํฌ๋„ŒํŠธ๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ HTML5์˜ History API๋ฅผ ์‚ฌ์šฉํ•ด ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์ง€ ์•Š๊ณ ๋„ ์ฃผ์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

๋˜ํ•œ <BrowserRouter> ๊ฐ€ ์ƒ์œ„์— ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด์•ผ React Router์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ReactDOM์˜ ๋ Œ๋” ๋‹จ๊ณ„์ธ index.js์—๋„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <BrowserRouter> //
      <App />
    </BrowserRouter> //
  </React.StrictMode>
);    



โœ” Routes, Route

๊ฒฝ๋กœ๋ฅผ ๋งค์นญํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค.

  • <Routes>

<Route> ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ์„œ ๊ทธ์ค‘ ๊ฒฝ๋กœ๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๋‹จ ํ•˜๋‚˜์˜ ๋ผ์šฐํ„ฐ๋งŒ ๋ Œ๋”๋ง์„ ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
<Routes> ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๋งค์นญ๋˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.


  • <Route>

path ์†์„ฑ์„ ์ง€์ •ํ•˜์—ฌ ํ•ด๋‹น path ์—์„œ ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณด์—ฌ์ค„์ง€ ์ •ํ•œ๋‹ค.
<Link> ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ •ํ•ด์ฃผ๋Š” URL ๊ฒฝ๋กœ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž‘๋™ํ•œ๋‹ค.


  • <Routes> ์™€ <Route> ๋กœ ์ฃผ์†Œ ๊ฒฝ๋กœ์™€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค€๋‹ค.
  • <Route>์˜ path ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๊ฒฝ๋กœ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • <Route> ํƒœ๊ทธ ์•ˆ์— element ์†์„ฑ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.



๊ฒฝ๋กœ๋ฅผ ์—ฐ๊ฒฐํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ, ํŽ˜์ด์ง€ ์ „ํ™˜์„ ํ†ตํ•ด ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜์—ฌ HTML5 History API๋ฅผ ์ด์šฉํ•ด ํŽ˜์ด์ง€์˜ ์ฃผ์†Œ๋งŒ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.


ReactDOM์œผ๋กœ ๋ Œ๋”๋ฅผ ์‹œํ‚ค๊ฒŒ ๋˜๋ฉด <Link> ์ปดํฌ๋„ŒํŠธ๊ฐ€ <a> ์š”์†Œ๋กœ ๋ฐ”๋€Œ๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

<a> ์š”์†Œ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ „ํ™˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ Œ๋”๋ง(์ƒˆ๋กœ๊ณ ์นจ ๋ฐœ์ƒ)์„ ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— React Router์—์„œ๋Š” <Link>๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
<Link> ์ปดํฌ๋„ŒํŠธ๋Š” ํŽ˜์ด์ง€ ์ „ํ™˜์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— SPA๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.


  • <Link> ์˜ to ์†์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ <Route> ์ปดํฌ๋„ŒํŠธ์— ์„ค์ •ํ•ด ์ค€ path ์ฃผ์†Œ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.




import logo from './logo.svg';
import './App.css';
import { BrowserRouter, Routes, Route, Link } from "react-router-dom"; // ์ด ๊ตฌ๋ฌธ์„ ๋„ฃ์–ด์ฃผ์„ธ์š”

function App () {
  return (
    // BrowserRouter๊ฐ€ ๊ฐ€์žฅ ์ƒ์œ„์— ์žˆ์–ด์•ผ ์‚ฌ์šฉ๊ฐ€๋Šฅ
    <BrowserRouter> 
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/mypage">MyPage</Link>
            </li>
            <li>
              <Link to="/dashboard">Dashboard</Link>
            </li>
          </ul>
        </nav>

        {/* ์ฃผ์†Œ ๊ฒฝ๋กœ์™€ ์•„๊นŒ ๋งŒ๋“  3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค๋‹ˆ๋‹ค. */}
        {/* Routes ์ปดํฌ๋„ŒํŠธ๋Š” Route ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๊ฐ์‹ธ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. */}
        <Routes>
          {/* ๊ฒฝ๋กœ๋Š” path๋กœ ์ปดํฌ๋„ŒํŠธ๋Š” element๋กœ ์—ฐ๊ฒฐํ•ด ์ค๋‹ˆ๋‹ค. */}
          <Route path='/' element={<Home />} />
          <Route path='/mypage' element={<MyPage />} />
          <Route path='/dashboard' element={<Dashboard />} />
        </Routes>

      </div>
    </BrowserRouter>
  )
}

export default App;

// Home ์ปดํฌ๋„ŒํŠธ
function Home() {
  return <h1>Home</h1>;
}

// MyPage ์ปดํฌ๋„ŒํŠธ
function MyPage() {
  return <h1>MyPage</h1>;
}

// Dashboard ์ปดํฌ๋„ŒํŠธ
function Dashboard() {
  return <h1>Dashboard</h1>;
}




Reference: ์ฝ”๋“œ์Šคํ…Œ์ด์ธ 

0๊ฐœ์˜ ๋Œ“๊ธ€