Skip to content

react router dom v6

J127_์šฐ์žฌ์„ edited this page Nov 14, 2021 · 2 revisions

react-router-dom์ด v5์—์„œ v6๋กœ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉด์„œ ๋ณ€๊ฒฝ๋œ ์ ์„ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

๋ฒˆ๋“ค ์‚ฌ์ด์ฆˆ ๊ฐ์†Œ

์ด์ „ ๋ฒ„์ „ ๋Œ€๋น„ ์•ฝ 70% ์ •๋„์˜ ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค์—ˆ๋‹ค.

https://reacttraining.com/static/ae12324448e8378c9090979a59269116/f43e4/v5-v6-bundle-size-comparison.png

switch โ†’ routes ๋„ค์ด๋ฐ ๋ณ€๊ฒฝ

๊ธฐ์กด์˜ route๋“ค์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๋ชจ ์š”์†Œ๋ฅผ switch ๋ผ๋Š” ๋„ค์ด๋ฐ์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ route์˜ ๋ณต์ˆ˜๊ฐœ๋ฅผ ๋œปํ•˜๋Š” routes๋กœ ์ด๋ฆ„์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.

// v5
<Switch>
  <Route ... />
</Routes>

// v6
<Routes>
  <Route ... />
</Routes>

exact ์˜ต์…˜ ์‚ญ์ œ

๊ธฐ์กด์˜ / ๋ผ์šฐํŠธ์˜ ๊ฒฝ์šฐ React Router์˜ ๋””ํดํŠธ ๋งค์นญ ๊ทœ์น™์œผ๋กœ ์ธํ•ด ์•ž๋ถ€๋ถ„๋งŒ ์ผ์น˜ํ•ด๋„ ์ „๋ถ€ ๋งค์นญ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •ํ™•ํžˆ ์ผ์น˜์‹œํ‚ค๊ณ ์ž exact ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์˜€์œผ๋‚˜, v6๋ถ€ํ„ฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋„๋ก ๋งค์นญ ๊ทœ์น™์ด ๋ณ€ํ•˜์—ฌ exact ์˜ต์…˜์€ ์‚ฌ๋ผ์กŒ๋‹ค. ๋งŒ์•ฝ v5์ฒ˜๋Ÿผ ํ•˜์œ„ ๊ฒฝ๋กœ์— ์—ฌ๋Ÿฌ ๋ผ์šฐํŒ…์„ ๋งค์นญ์‹œํ‚ค๊ณ ์ž ํ•œ๋‹ค๋ฉด *๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

// categores ๋กœ ์‹œ์ž‘๋˜๋Š” ๋ชจ๋“  ๋ผ์šฐํŒ… ๋งค์นญ
<Route path='categories/*' />

component โ†’ element๋กœ

Route ์ปดํฌ๋„ŒํŠธ์˜ component ์†์„ฑ์ด ์‚ฌ๋ผ์ง€๊ณ  element ์†์„ฑ์ด ์ƒ๊ฒผ๋‹ค.

// v5
<Route path="/" component={Home} />

// v6
<Route path="/" element={<Home />} />

์ƒ๋Œ€ ๊ฒฝ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

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

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="users/*" element={<Users />} />
      </Routes>
    </BrowserRouter>
  );
}

function Users() {
	/* <Users> element๋Š” URL์ด /USERS/*์™€ ์ผ์น˜ํ•  ๋•Œ ๋ Œ๋”๋ง๋˜๊ธฐ ๋•Œ๋ฌธ์—
		 <Route path> ์™€ <Link to> ๊ฐ’์€ ์ž๋™์œผ๋กœ /users URL ์ ‘๋‘์‚ฌ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.
	*/
  return (
    <div>
      <nav>
        <Link to="me">My Profile</Link>
      </nav>

      <Routes>
        <Route path="/" element={<UsersIndex />} />
        <Route path=":id" element={<UserProfile />} />
        <Route path="me" element={<OwnUserProfile />} />
      </Routes>
    </div>
  );
}

๋”ฐ๋ผ์„œ v5์—์„œ ์ฒ˜๋Ÿผ match.path๋‚˜ match.url์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๊ทธ ๋ฐ–์—๋„ ๋งŽ์€ ๊ฒƒ์ด ์ถ”๊ฐ€๋˜๊ณ  ๋‹ฌ๋ผ์กŒ๋‹ค.

์ฐธ์กฐ

React Training: React Router v6 Preview

react-router v6์—์„œ๋Š” ์–ด๋–ค๊ฒƒ๋“ค์ด ๋ณ€ํ–ˆ์„๊นŒ?? (woolta.com)

๐Ÿ“– ๊ฐœ๋ฐœ๋ฌธ์„œ

๐Ÿšฅ ๊ทœ์น™

๐Ÿค” ์Šคํ”„๋ฆฐํŠธ ํšŒ์˜

๐Ÿ“” ํ•™์Šต

๐Ÿ•™ ๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ

๐Ÿ’ญ ํšŒ๊ณ ๋ก

๐Ÿ‘จโ€๐Ÿ‘ฆ ๋ฉ˜ํ† ๋ง

๋ฐ๋ชจ์˜์ƒ

Clone this wiki locally