React Router v6
Setup
React doesn't come with changing component rendered based on url out of the box, so need react-router-dom
npm install react-router-dom
import { BrowserRouter, Route, Routes } from "react-router-dom";Use HashRouter for within filesystems(electron)
Routers
Need BrowserRouter outer component
Will render any number of regex matches of child Routes
Basically conditional rendering based on path
Routes
<Routes> groups Route components, and will choose the most specific route so /event/login matches /event/login over /event/:id
<BrowserRouter>
<Routes>
<Route path="/" element={<MyRedirect url="/login" />} />
<Route path="/login" element={<Login />} />
<Route path="/" element={<UserRoute requiredOrganizer />}>
<Route path="/create-event" element={<CreateEvent />} />
<Route
path="/dashboard/*"
element={<DashboardRouter />}
/>
</Route>
</Routes>)Navigation
Link
Navlinks add style when active, classes, and on actived event
Redirect
HTML5 way
Accessing Router Info
Route with parameter
Access Data
Advanced
URL Path
Redirect to Route
Redirect to External Route
MyRedirect.tsx
Usage
Last updated