Welcome to our in-depth guide on React Router! By the end of this tutorial, you'll master the art of creating dynamic, multi-page applications using React and React Router. Let's dive in!
React Router is a popular library used for routing in React applications. It helps you manage different components, URLs, and user navigation.
Why use React Router? Imagine building a website with multiple pages without routing. That would mean writing a new component for each URL, which can quickly become cumbersome. React Router simplifies this process by allowing you to define routes and map them to components.
To get started, first, make sure you have React and Node.js installed. After creating a new React app, install React Router using npm:
npm install react-router-domIn your app's main component (App.js), import the necessary modules:
import { BrowserRouter, Route, Switch } from 'react-router-dom';Now, let's create routes for our application. Wrap your components inside <BrowserRouter> and <Switch> components:
function App() {
return (
<BrowserRouter>
<div className="App">
<Switch>
{/* Define routes here */}
</Switch>
</div>
</BrowserRouter>
);
}Each <Route> component represents a unique path in your application. Provide a path and a component for each route:
<Route path="/home" component={Home} />
<Route path="/about" component={About} />Nested routes can help you create a multi-level hierarchy of components. For example, to create a nested route for a user profile:
<Route path="/user/:id" component={UserProfile} />Params are dynamic segments of a route's path. Use them to create dynamic routes:
<Route path="/post/:id" component={Post} />Access the param value in your component:
function Post({ match }) {
const { params } = match;
const postId = params.id;
// Do something with postId
}Use exact to ensure that the route exactly matches the provided path:
<Route exact path="/home" component={Home} />To create links for navigation, use the <Link> component:
<Link to="/home">Home</Link>
<Link to="/about">About</Link>Question: What does <Switch> component do in a React Router setup?
A: It defines routes B: It helps manage components and URLs C: It handles navigation
Correct: B
Explanation: The <Switch> component helps manage components and URLs by ensuring only one <Route> renders at a time for a given path.
Build a simple React application with three pages: Home, About, and Contact Us. Create a new file for each component and include them in the <Switch> component.
// App.js
import React from 'react';
import Home from './Home';
import About from './About';
import ContactUs from './ContactUs';
function App() {
return (
<BrowserRouter>
<div className="App">
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={ContactUs} />
</Switch>
</div>
</BrowserRouter>
);
}
export default App;// Home.js
function Home() {
return <h1>Welcome to Home Page!</h1>;
}
// About.js
function About() {
return <h1>About Us</h1>;
}
// ContactUs.js
function ContactUs() {
return <h1>Contact Us</h1>;
}Now, create navigation links in a new file Navbar.js:
import React from 'react';
import { Link } from 'react-router-dom';
function Navbar() {
return (
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact Us</Link></li>
</ul>
</nav>
);
}
export default Navbar;Use the Navbar component in the App component:
// App.js
import Navbar from './Navbar';
// ...
function App() {
return (
<BrowserRouter>
<Navbar />
<div className="App">
<Switch>
// ...
</Switch>
</div>
</BrowserRouter>
);
}
export default App;That's it! You've created a multi-page React application using React Router. Happy coding! 🎉