

Basic routing in React using React Router
source link: https://www.tuicool.com/articles/hit/ZVZFJzv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

React is widely used library for client side web applications. In any web applications, there will be multiple pages. routing the URL properly and load different pages based on route parameters is a general requirement.
There is an awesome npm package which takes all the complexity to serve the purpose of routing in React. react-router-dom
is one of the widely used react library.
Basic routing
Lets create two simple pages
/ /about
Create a simple react app using create-react-app
CLI. Its very easy with npx - npx create-react-app my-react-app
// App.js import React from 'react'; const App = () => { return ( <section className="App"> <h1>React routing Example</h1> </section> ); }; export default App;
Lets create two pages. In simple terms two functional react component.
// App.js ... const IndexPage = () => { return ( <h3>Home Page</h3> ); }; const AboutPage = () => { return ( <h3>About Page</h3> ); }; ...
Before diving deep into react router code, First lets understand, what are all needed for routing a page in react application.
- There will be links to navigate between pages.
- Define Route to the pages. It define the URL path and component to load for the URL.
- Define a Router which will check whether the requested URL exist in the defined Routes.
Lets create the links and routes using react router’s Link
and Route
components. First install the package yarn add react-router-dom
.
// App.js ... import { Link, Router as BrowserRouter, Route } from 'react-router-dom';... const App = () => { return ( <section className="App"> <Router> <Link to="/">Home</Link> <Link to="/about">About</Link> <Route path="/" component={IndexPage} /> <Route path="/about" component={AboutPage} /> </Router> </section> ); };
Let’s go through each line separately
import { Link, Router as BrowserRouter, Route } from 'react-router-dom';
Here we are importing three components,
Link Route Router
BrowserRouter is one type of router, it is also the widely used router. It uses HTML5 push state underneath the component to route your pages. We will discuss in more details about different types of router later in this series.
// Link with URL <Router> <Link to="/">Home</Link> <Link to="/about">About</Link> </Router>
Router
should be the parent component enclosing Link
and Route
. So that it can handle the routing. If we place the Link or Route outside it won’t work. It will throw an error.
Link
accept to
props which defines the URL it want to link.
Why do we need Link component, why not a HTML anchor tag with href?
a
// Route with definition <Route path="/" component={IndexPage} />
Here Route
have path and component props. component
props helps to render the component when user comes to this route. path
props define the url path to be matched when user visits the page.
If you go ahead and check whether our routes are working, it will work. But it have a small glitch.
If you click about link, it will render both IndexPage
and AboutPage
component in its page. Why
Because the path defined for about is /about
. Here router traverses through the route definitions from top to bottom. First checks the Route with path /
and the about URL have /
, so it renders IndexPage component first. And then it checks the next Route /about
, that also matches, so it renders AboutPage component.
How to match exact route?
Its very simple, the question itself have the answer :sunglasses:. Use exact
props in Route.
... const App = () => { return ( <section className="App"> <Router> <Link to="/">Home</Link> <Link to="/about">About</Link> <Route exact path="/" component={IndexPage} /> <Route exact path="/about" component={AboutPage} /> </Router> </section> ); }; ...
exact
prop will help to match the route only if the whole route matches as it is, else it won’t render the component.
Now both the component will render fine and the Link will work properly.
Thats all folks, you have already completed the part 1 of Deep dive into React Router series. Hope you enjoyed and learned few things for your next big react app
You can checkout the codebase for this series here and the code for this section here
Recommend
-
149
Welcome to React Router · React Router is a lightweight, fully-featured routing library for the React JavaScript library. React Router runs everywhere that React runs; on the web, on th...
-
50
README.md
-
27
README.md
-
6
Routing Angular 2 Single Page Apps with the Component Router ...
-
15
React Router Declarative routing for React View the docs here
-
9
File-based routing with React RouterFile-based routing with React Router - 1September 08, 2021Most React applications involve a routing setup to allow user navigation between different pages. There are two common ways to handle...
-
14
Let’s take another look at routing in Angular—this time how you can use the router link to set up wildcards, so nonexistent URLs bring up a 404 page or redirect users to another page. In this post, we will look into more things you can...
-
13
Go HTTP Router Benchmark This benchmark suite aims to compare the performance of HTTP request routers for Go by implementing the routing structure of some real world APIs. Some of the APIs are slight...
-
7
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
2
BackRouting with React Router 6June 10th, 2022 · 7 min read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK