μ€μΉ
yarn add react-router-dom
β createBrowserRouter()
const router = createBrowserRouter([κ° κ²½λ‘μ λ°°μ΄])
App.js
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
const router = createBrowserRouter([]);
export default function App() {
return <RouterProvider router={router} />
}
- μμ κ°μ΄ μμ μ»΄ν¬λνΈμ μΈν μ ν΄μ€λλ€.
μ»΄ν¬λνΈ κ²½λ‘μ μΆκ°νκΈ°
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
const router = createBrowserRouter([
{
path : '/',
element : <p>Home</p>
},
]);
export default function App() {
return <RouterProvider router={router} />
}
- κ²½λ‘κ° μ¬λ°λ₯΄κ² μ‘ν μλ κ²μ λ³Ό μ μμ΅λλ€.
νμ§λ§, λ§μ½ κ²½λ‘κ° μλͺ» λμ΄μκ±°λ μμΌλ©΄ β
κ²½λ‘κ° μλ€κ³ λ§μ ν΄μ€λλ€. μ΄μ μ°λ¦¬λ , λ λμ UX λ₯Ό μν΄μ μλ¬κ° λ¬μ λ 보μ¬μ€ νλ©΄μ λͺ μ ν μ μμ΅λλ€.
β errorElement
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
const router = createBrowserRouter([
{
path : '/',
element : <p>Home</p>,
errorElement : <p>Not Found</p>
}
]);
export default function App() {
return <RouterProvider router={router} />
}
- κ²½λ‘κ° μλͺ» λμμλ μ¬μ©μμκ² λ³΄μ¬μ€ νμ΄μ§λ₯Ό μ€μ ν μ μμ΅λλ€.
β outlet (μμΈλ ) μ¬μ©νκΈ°
μ¬μ΄νΈμ navbar(λ€λΉκ²μ΄μ ) κ° μκ³ ν΄λΉ νλ λ©λ΄λ₯Ό λλ μ λ navbar λ κ·Έλλ‘ μ μ§νλ©΄μ ν΄λΉνλ λ΄μ©λ§ μ ννκ³ μΆμ λ μ¬μ© ν μ μμ΅λλ€.
- μ 체μ μΈ λ μ΄μμμ μ‘μ λ μ μ©νκ² μ¬μ© ν μ μμ΅λλ€.
νμΌ κ΅¬μ‘°
src-pages
App.js
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './pages/Home';
import NotFound from './pages/NotFound';
import Root from './pages/Root';
import Video from './pages/Video';
const router = createBrowserRouter([
{
path : '/',
element : <Root/>,
errorElement : <NotFound/>,
children : [
{index : true, element : <Home/>},
{path : '/video', element : <Video/>}
]
}
]);
export default function App() {
return <RouterProvider router={router} />
}
- ν΄λΉ Root element μμ μλ μμΈλ μμ 보μ¬μ£ΌκΈΈ μνλ€λ©΄ κ²½λ‘λ€μ μμκ°μ΄ μμμΌλ‘ λ§λ€μ΄μΌ ν©λλ€. (children)
- index μμ±μ κΈ°λ³Έμ μΌλ‘ 보μ¬μ€ νμ΄μ§ μ λλ€.
- router λΆλΆμ μ€λͺ νμλ©΄, Root μμ μμ μμμ΄ μλλ° κ·Έ μ€ μ΅μμ κ²½λ‘(index) λ κΈ°λ³Έμ μΌλ‘ Home μ΄κ³
- κ²½λ‘κ° video λΌλ©΄, video μ»΄ν¬λνΈλ₯Ό 보μ¬μ€λλ€.
Root.js
import React from 'react'
import { Outlet } from 'react-router-dom'
import NavBar from '../components/NavBar'
export default function Root() {
return (
<div>
<NavBar/>
<Outlet/>
</div>
)
}
NavBar.js
import React from 'react'
import { Link } from 'react-router-dom'
export default function NavBar() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/video">Video</Link>
</nav>
)
}
- Link λ μμ ν¬μ€ν
μμ μΈκΈν μ μ΄ μμΌλ―λ‘, 리μ‘νΈμμ μ¬μ©νλ CSR λ₯Ό μν a νκ·ΈλΌκ³ 보μλ©΄ λ©λλ€.
β url μ param μ λ¬νκΈ°
import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './pages/Home';
import NotFound from './pages/NotFound';
import Root from './pages/Root';
import Video from './pages/Video';
import VideoDetail from './pages/VideoDetail';
const router = createBrowserRouter([
{
path : '/',
element : <Root/>,
errorElement : <NotFound/>,
children : [
{index : true, element : <Home/>},
{path : '/video', element : <Video/>},
{path : '/video/:id', element : <VideoDetail/>}
]
}
]);
export default function App() {
return <RouterProvider router={router} />
}
- id μ κ°μ λ°λΌ λ€λ₯Έ μμΈ νμ΄μ§λ₯Ό λ§λ€ μ μμ΅λλ€. ( νλΌλ―Έν°μ λ°λ₯Έ νμ΄μ§ )
Video.js
import React from 'react'
import { useState } from 'react'
import { useNavigate } from 'react-router-dom';
export default function VideoDetail() {
// navigate
const navigate = useNavigate();
const [text, setText] = useState('');
const handleChange = (e) => {
setText(e.target.value);
}
const handleSubmit = (e) => {
e.preventDefault();
setText('');
navigate(`/video/${text}`);
}
return (
<div>
Video
<form onSubmit={handleSubmit}>
<input type='text' placeholder="video id : " value={text} onChange={handleChange}/>
</form>
</div>
)
}
- input μ μ λ ₯ν id λ‘ νμ΄μ§λ₯Ό μ λ°μ΄νΈ νλ μ½λ μ λλ€.
β useNavigate
- 리μ‘νΈ λΌμ°ν°μμ νΉμ ν κ²½λ‘λ‘ μ΄λ ν μ μλ λ°©λ²μ λ κ°μ§κ° μλλ°
- κ·Έ μ€ νλλ Link νκ·Έλ₯Ό μ΄μ©ν΄μ μ¬μ©μκ° μνΈμμ© νμ λ ν΄λΉνλ Url λ‘ μ΄λνλ κ²
- κ·Έλ¦¬κ³ , μ½λ μμμ λμ μΌλ‘ μ΄λνκ³ μΆλ€λ©΄ navigate λ₯Ό μ¬μ© ν©λλ€.
β useParams
- μ λ¬λ°μ λ°μ΄ν°λ₯Ό λ°μμ 보μ¬μ£ΌκΈ°
- URL μΈμλ€μ key/value(ν€/κ°) μ§λ€μ κ°μ²΄λ₯Ό λ°ν
VideoDetail.js
import React from 'react'
import { useParams } from 'react-router-dom'
export default function VideoDetail() {
const {id} = useParams();
return (
<div>
VideoDetail {id}
</div>
)
}
- { id } μ΄ λΆλΆμ λΌμ°ν°λ₯Ό λ§λ€ λ {path : '/video/:id', element : } μ¬κΈ°μ id λΌκ³ λͺ μλ₯Ό νμ΅λλ€.
- λͺ μν κ²½λ‘μ id μ ν΄λΉ url μ κ°μ΄ λ€μ΄μ€κ² λλ κ² μ λλ€.
'π Front-End > React.js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
React + TS + Style-Components (0) | 2022.11.15 |
---|---|
React-Query μΊμ±μ λνμ¬ (0) | 2022.11.12 |
React + TypeScript(state) (0) | 2022.10.26 |
React + TypeScript(Optional Props) (0) | 2022.10.26 |
React + TypeScript(interface) (0) | 2022.10.26 |