YouTip LogoYouTip

React Blog Router

## Routing Jump In this chapter, you will learn how to implement refresh-free page navigation using React Router v6, including dynamic routing and parameter reading. * * * ## Why Use Routing? Traditional multi-page websites require requesting a new page from the server each time a link is clicked, resulting in white screen flickering. Frontend routing allows JS to control which component to display when the URL changes, without sending requests to the server. Benefits: Smooth transitions, experience like a native APP. * * * ## React Router v6 Core Concepts | Concept | Purpose | Corresponding Code | | --- | --- | --- | | Route Table | Defines URL to component mapping | ` + ` | | Route Outlet | Placeholder position for sub-route display | `` | | Navigation Link | Click to navigate | `` | | Dynamic Routing | Variables in URL | `/post/:id` | | Reading Parameters | Get values from dynamic routes | `useParams()` | * * * ## Installation and Configuration $ npm install react-router-dom@6 ### Create Route Configuration ## Example // File path: src/router/index.jsx import{ createBrowserRouter } from 'react-router-dom' import App from '../App' import HomePage from '../pages/HomePage' import PostPage from '../pages/PostPage' // createBrowserRouter create router instance const router = createBrowserRouter([ { path:'/',// App as the root layout element:, children:[ { index:true,// index: true represents the default child route for the / path element: }, { path:'post/:id',// Dynamic Route::id matches any value element: } ] } ]) export default router > Two routing modes: createBrowserRouter produces clean URLs (/post/1), requiring server configuration. If the deployment environment is uncertain, use createHashRouter, which includes # in the URL (/#/post/1) and requires no server configuration. ### Register Routes in main.jsx ## Example // File path: src/main.jsx import React from 'react' import ReactDOM from 'react-dom/client' import{ RouterProvider } from 'react-router-dom' import router from './router' import'./index.css' ReactDOM.createRoot(document.getElementById('root')).render( {/* RouterProvider inject the router into the application */} ) ### Use Outlet in App.jsx ## Example // File Path: src/App.jsx import{ Outlet } from 'react-router-dom' import NavBar from './components/NavBar' import'./App.css' function App(){ return(
{/* Outlet is the outlet for nested routes: components matched by child routes will be rendered here */}

Β© 2024 TUTORIAL Blog. Powered by React.

) } export default App * * * ## Link β€” Declarative Navigation Link replaces the traditional `` tag and does not trigger page refresh. ## Example import{ Link } from 'react-router-dom' function BlogCard({ id, title, summary, date, category }){ return(
{category}

{title}

{summary}

{date}
) } > Don't use `
` β€” it will cause the browser to request a new page from the server, and the entire React application will reinitialize. Use `` to switch on the client side without refresh. * * * ## useParams β€” Reading Route Parameters In the detail page, you need to extract the article ID from the URL to find the corresponding article. ## Example import{ useParams } from 'react-router-dom' function PostPage(){ // useParams returns an object containing the parameters from the URL const{ id }= useParams() // Note: id is a string, use Number(id) to convert when a number is needed return(

Current article ID:{id}

) } * * * ## useNavigate β€” Programmatic Navigation In some scenarios, you need to navigate without clicking a link (e.g., after form submission, or when article not found to redirect back to homepage). ## Example import{ useNavigate } from 'react-router-dom' function PostPage(){ const navigate = useNavigate() const article =null// assuming the article is not found if(!article){ // Post not foundredirect to homepage return(

Post not found

) } return
Article Details...
} ### Vue3 vs React Routing API Comparison | Feature | Vue3 | React | | --- | --- | --- | | Navigation Link | `` | `` | | Route Outlet | `` | `` | | Dynamic Routing | `/post/:id` | `/post/:id` | | Reading Parameters | `useRoute().params` | `useParams()` | | Programmatic Navigation | `useRouter().push()` | `useNavigate()` | * * * ## Hands-on: Complete Detail Page + Route Integration ### Step 1: Modify BlogCard to Support Click Navigation ## Example // File path: src/components/BlogCard.jsx import{ Link } from 'react-router-dom' function Blog
← React Blog SearchReact Blog Usestate β†’