When building modern web applications with React, it’s essential to manage navigation between various components or "pages." Enter React Router, a standard library for routing in React apps that helps developers implement dynamic navigation without the hassle. This blog is meant to demystify how React Router works, guiding you through setting up routes and using them effectively.
React Router is a declarative routing library for React. It lets you define routes in a single place and makes it easy to navigate between different components. Each route corresponds to a component that renders when the current URL matches its path. This approach makes it possible to have a multi-page feel within a single-page application (SPA).
To get up and running with React Router, you'll need to install it in your React project. If you haven't created a React app yet, you can start by running:
npx create-react-app my-app cd my-app npm install react-router-dom
Once you have React Router installed, you can start defining routes. In your main App.js
file, you can set up your router like so:
import React from 'react'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <div> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route component={NotFound} /> </Switch> </div> </Router> ); } export default App;
Route
component matches a specific path to a component that should be rendered when the path matches.Switch
component renders the first child Route
that matches the path. If no paths match, it can render a fallback component, which in this case is NotFound
.Now, let’s create the components we referenced above.
Home.js:
import React from 'react'; function Home() { return <h1>Welcome to the Home Page</h1>; } export default Home;
About.js:
import React from 'react'; function About() { return <h1>About Us</h1>; } export default About;
NotFound.js:
import React from 'react'; function NotFound() { return <h1>404 - Not Found</h1>; } export default NotFound;
To navigate between these pages, we’ll need to use the Link
component from React Router. Here's how you can update your App.js
to include navigation links:
import React from 'react'; import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom'; import Home from './Home'; import About from './About'; import NotFound from './NotFound'; function App() { return ( <Router> <nav> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> </ul> </nav> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route component={NotFound} /> </Switch> </Router> ); } export default App;
React Router also allows for nested routing, meaning that you can have routes within routes. Let’s say you want to add more details under the "About" page. You can create a new component for "Team" details and set it up as a nested route in About.js
.
Team.js:
import React from 'react'; function Team() { return <h2>Meet Our Team</h2>; } export default Team;
Now modify About.js
to include the nested route:
import React from 'react'; import { Route, Link, Switch } from 'react-router-dom'; import Team from './Team'; function About() { return ( <div> <h1>About Us</h1> <nav> <ul> <li><Link to="/about/team">Our Team</Link></li> </ul> </nav> <Switch> <Route path="/about/team" component={Team} /> </Switch> </div> ); } export default About;
Now if you run your application and navigate to the "About" page, you will see a link for "Our Team." Clicking this link will render the Team
component while keeping the About
header intact. This organization promotes component reuse and a clear structure within your application.
By leveraging React Router, developers can create intuitive user experiences that navigate seamlessly between pages while maintaining all the benefits of a single-page application.
24/08/2024 | ReactJS
14/09/2024 | ReactJS
24/08/2024 | ReactJS
14/09/2024 | ReactJS
24/08/2024 | ReactJS
13/07/2024 | ReactJS
14/09/2024 | ReactJS
14/09/2024 | ReactJS
28/11/2024 | ReactJS