Home >Web Front-end >JS Tutorial >Understanding React Router vFeatures, Setup, and Best Practices
React Router is the most popular library used for routing in React applications. It allows you to navigate between different components based on the URL, providing a single-page application (SPA) experience where the content updates without reloading the entire page. React Router v6 is the latest major release and introduces a range of improvements, simplifications, and new features compared to previous versions.
Simplified API
Route Element (element)
Route Matching
Nested Routes
No More exact
React Router Hooks
To get started with React Router v6, follow these steps:
You can install React Router v6 using npm or yarn.
npm install react-router-dom@6 # or yarn add react-router-dom@6
Let’s look at an example of how to set up basic routing in React Router v6.
npm install react-router-dom@6 # or yarn add react-router-dom@6
Home.js
import React from 'react'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; const App = () => { return ( <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="*" element={<NotFound />} /> </Routes> </Router> ); }; export default App; <p><strong>About.js</strong><br> </p> <pre class="brush:php;toolbar:false">import React from 'react'; const Home = () => { return <h2>Home Page</h2>; }; export default Home;
NotFound.js
import React from 'react'; const About = () => { return <h2>About Us</h2>; }; export default About;
To handle dynamic routes, such as a user profile page where the user ID is part of the URL, React Router v6 provides the useParams hook.
import React from 'react'; const NotFound = () => { return <h2>Page Not Found</h2>; }; export default NotFound;
Nested routes allow you to build complex layouts by defining sub-routes within parent routes.
import React from 'react'; import { Routes, Route, useParams } from 'react-router-dom'; const UserProfile = () => { const { userId } = useParams(); // Extract the userId from the URL return <h2>User Profile for ID: {userId}</h2>; }; const App = () => { return ( <Routes> <Route path="/user/:userId" element={<UserProfile />} /> </Routes> ); }; export default App;
React Router v6 introduces several hooks for navigating and accessing routing information:
The useNavigate hook allows programmatic navigation within your application.
npm install react-router-dom@6 # or yarn add react-router-dom@6
React Router v6 introduces several improvements over previous versions, including a simpler API, better route matching, and enhanced support for dynamic and nested routes. By leveraging hooks like useNavigate, useParams, and useLocation, you can build powerful and flexible routing systems in your React applications.
The above is the detailed content of Understanding React Router vFeatures, Setup, and Best Practices. For more information, please follow other related articles on the PHP Chinese website!