undefined

 

2021๋…„์— React๋ฅผ ์ด์šฉํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ์—ˆ๋‹ค. (๋‚ด ์ฒซ ํ”„๋ก ํŠธ์—”๋“œ ํ”„๋กœ์ ํŠธ)

๊ทธ๋•Œ ์ดํ›„๋กœ ํšŒ์‚ฌ์— ์ž…์‚ฌํ•˜๋ฉด์„œ Next.js๋กœ๋งŒ ๊ฐœ๋ฐœํ•˜๋‹ค ๋ณด๋‹ˆ react-router-dom๋ฅผ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์—ˆ๋‹ค.

Next.js๋Š” pages ํด๋”๋ฅผ ์ด์šฉํ•ด ์ž๋™์œผ๋กœ ๋ผ์šฐํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๋ฆฌ์•กํŠธ ๋ฉ˜ํ† ๋ง์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ 2๋…„๋งŒ์— ๊ธฐ์–ต ์ €ํŽธ์˜ react-router-dom์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•ด ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

2๋…„ ์ „์— ์‚ฌ์šฉํ–ˆ๋˜ ๋ฒ„์ „์€ 5.2.0์ด๊ณ  ์ด๋ฒˆ์— ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ ๋ฒ„์ „์€ 6.10.0์ด๋‹ค.

 

v6๋กœ ์—…๋ฐ์ดํŠธ๋˜๋ฉด์„œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์™„์ „ํžˆ ๋ฐ”๋€Œ์—ˆ๋‹ค.


react-router-dom BroswerRouter๋กœ ๋ผ์šฐํ„ฐ ๊ตฌํ˜„ํ•˜๊ธฐ

v5์—์„œ ๋ผ์šฐํŒ… ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

๊ธฐ์กด v5์—์„œ๋Š” <Switch />๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์กด์žฌํ–ˆ๋‹ค.

<BrowserRouter>
    <Switch>
        <Route path="/" component={HomePage} />
        <Route exact path="/posts/:id" component={PostPage} />
    </Switch>
</BrowserRouter>

path prop์— `/posts/:id` ์ฒ˜๋Ÿผ `:<path parameter>` ๋กœ ๋‹ค์ด๋‚˜๋ฏน ๋ผ์šฐํŒ…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๊ฐ’์€ Route์˜ ์ปดํฌ๋„ŒํŠธ์—์„œ(PostPage) props๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

export default function PostPage({ match, location, history }) {
    const id = match.params.id;
    const routeToSomewhere = () => {
        history.push(somewhere);
    };
    ...
}

 

v6์—์„œ ๋ผ์šฐํŒ… ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

ํฌ๊ฒŒ ๋‹ฌ๋ผ์ง„ ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

  1. <Switch /> ์ปดํฌ๋„ŒํŠธ๊ฐ€ <Route />๋กœ ๊ต์ฒด๋จ
  2. component prop ๋Œ€์‹  element prop์„ ์‚ฌ์šฉ.
  3. route ๋Œ€์ƒ์ธ ์ปดํฌ๋„ŒํŠธ์— ์–ด๋–ค prop๋„ ์ „๋‹ฌํ•˜์ง€ ์•Š์Œ.
<BrowserRouter>
    <Routes>
        <Route path='/' element={<HomePage />} />
        <Route exact path='/posts/:id' element={<PostPage />} />
    </Routes>
</BrowserRouter>

 

๋”์ด์ƒ prop์œผ๋กœ match, history, location์ด ์ „๋‹ฌ๋˜์ง€ ์•Š๋Š”๋‹ค.

๋Œ€์‹  useParams์™€ useNavigate์™€ ๊ฐ™์€ Hook์œผ๋กœ ๋ผ์šฐํŒ…์„ ๊ตฌํ˜„ํ•œ๋‹ค.

export default function PostPage() {
    const { id } = useParams()
    const navigate = useNavigate()

    const routeToSomewhere = () => {
        navigate(somewhere)
    }
}

 


 

๋งˆ๋ฌด๋ฆฌ

๋ฆฌ์•กํŠธ๋Š” 16.8 ๋ฒ„์ „ ๋•Œ๋ถ€ํ„ฐ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ง€์›ํ•˜๊ณ  Hook ๊ฐœ๋…์„ ๋„์ž…ํ•˜์˜€๋‹ค.

๋ฆฌ์•กํŠธ๊ฐ€ ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋กœ ๋„˜์–ด๊ฐ์— ๋”ฐ๋ผ react-router-dom๋„ v6๋ถ€ํ„ฐ ๊ต‰์žฅํžˆ ๋งŽ์€ Hook์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

 

์˜ˆ์ „์—๋Š” path parmeter๋ฅผ match prop์œผ๋กœ ๋„˜๊ฒจ์คฌ๊ธฐ ๋•Œ๋ฌธ์— prop drilling์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ผ์–ด๋‚ฌ๋‹ค.

์ด๋ฅผ useParams Hook์œผ๋กœ ๊ต์ฒดํ•˜๋ฉด์„œ ์•„๋ฌด๋ฆฌ ๊นŠ์€ ๋ ˆ์ด์–ด์— ์žˆ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ผ๋„ prop drilling ์—†์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์ถ”๊ฐ€๋กœ ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ์‹ถ์€ ๊ฒƒ

v6์—์„œ <Route />์— ๋ช‡ ๊ฐ€์ง€ props๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋Š”๋ฐ, loader๊ฐ€ ๊ฐ€์žฅ ๋ˆˆ์— ๋„์—ˆ๋‹ค.

loader๋Š” navigate ์‹œ ๋ฐ์ดํ„ฐ ํŒจ์นญ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

๋ณดํ†ต path parameter๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ ์ด์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ Route์—์„œ ๋ฐ”๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•  ๊ฒƒ ๊ฐ™๋‹ค.