Introduction
In the previous articles, we have learned about Redux integration with React. Now we will learn about Routing in React. In this article, we will learn what is React routing and how it can be implemented in our application.
React Router
React router is a routing library built on top of the react used to create the routing in react apps.
Initially React router v4 was static. After React router v4 routings are dynamic. While using single-page applications, the single HTML page is used and multiple components are re-rendered in routing, but while using multiple pages in the application an entirely new page is re-rendered.
Setup React Router
To create a routing application, we need a command named
- npx create-react-app routingapp
After successful creation of react application, change your folder path to your application
and after that, we will install a react-router package to start using it in our application.
- npm install react-router-dom
After the successful installation of the above package, now we will create 2 components named Home and ContactUs.
Home.js
- import React, { Component } from 'react'
-
- export class Home extends Component {
- render() {
- return (
- <div>
- Home Component
- </div>
- )
- }
- }
-
- export default Home
ContactUs.js
- import React, { Component } from 'react'
-
- export class ContactUs extends Component {
- render() {
- return (
- <div>
- Contact Us Component
- </div>
- )
- }
- }
-
- export default ContactUs
There are two types of routers in React
- BrowserRouter - When your application needs to handle dynamic requests than BrowserRouter is used.
- HashRouter - When static request needs to be handled by your application then HashRouter is used.
The React router provides three components, Route, Link, BrowserRouter which help in the routing of the component.
Let’s implement routing in App.js. For that first, we need to import component from react Router along with our custom-created component,
- import { Route, Link, BrowserRouter as Router } from 'react-router-dom'
-
- import Home from './components/Home';
-
- import ContactUs from './components/ContactUs';
After importing we will define <Router> component under which we will link our component using <Link> Component and define route using <Route> component as below,
- <Router>
- <div className="App">
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><Link to={'/'} className="nav-link"> Home </Link></li>
- <li><Link to={'/ContactUs'} className="nav-link">Contact Us</Link></li>
-
- </ul>
- </nav>
- <hr />
- <Route exact path="/" component={Home} />
- <Route path="/" component={ContactUs} />
- </div>
- </Router>
Now the whole component will look like as below,
- import React from 'react';
- import { Route, Link, BrowserRouter as Router } from 'react-router-dom'
- import './App.css';
- import Home from './components/Home';
- import ContactUs from './components/ContactUs';
-
- function App() {
- return (
- <Router>
- <div className="App">
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><Link to={'/'} className="nav-link"> Home </Link></li>
- <li><Link to={'/ContactUs'} className="nav-link">Contact Us</Link></li>
-
- </ul>
- </nav>
- <hr />
- <Route exact path="/" component={Home} />
- <Route path="/" component={ContactUs} />
- </div>
- </Router>
- );
- }
-
- export default App;
Now we will create another file named main.js,
- import React from 'react'
- import App from './App'
-
- function main() {
- return (
- <div>
- <App></App>
- </div>
- )
- }
-
- export default main
Now after running the application, it will look like below,
In Route component, it accepts 2 parameters:
path: It requires the path of the component
component: It accepts the name of the component needed for navigation.
Now let’s think of a case where the URL for which a user wants to navigate does not found. In that case, we need to implement a 404 not found page.
What is 404 Page and how it can be implemented?
A 404-page also called as Not found page which is required whenever the user navigates to a wrong page or the page that does not exist. In that case, the user is shown a Not found page.
To add a 404 page in React, we will need another component named Switch.
Switch component helps us to render the component only when path found else it will go to not found component.
To create a not found component, we will create a notfound.js component,
- import React from 'react'
-
- const NotFound = () => <h1>Page Not Found</h1>
-
- export default NotFound
Now import in App.js,
- import React from 'react';
- import { Route, Link, Switch, BrowserRouter as Router } from 'react-router-dom'
- import './App.css';
- import Home from './components/Home';
- import ContactUs from './components/ContactUs';
- import NotFound from './components/notfound';
- function App() {
- return (
- <Router>
- <div>
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><Link to={'/'} className="nav-link"> Home </Link></li>
- <li><Link to={'/ContactUs'} className="nav-link">Contact Us</Link></li>
- </ul>
- </nav>
- <hr />
- <Switch>
- <Route exact path="/" component={Home} />
- <Route path="/contactus" component={ContactUs} />
- <Route component={NotFound} />
- </Switch>
- </div>
- </Router>
- );
- }
-
- export default App;
and the output will be displayed as, hit the link, http://localhost:3000/users
as we know the user component does not exist so it will display a 404 page.
URL Parameters
Url parameters are used to render the same components along with dynamic url. Just as passing name in ContactUs component like below,
- <Route path="/contactus/:name" component={ContactUs} />
App.js
- import React from 'react';
- import { Route, Link, Switch, BrowserRouter as Router } from 'react-router-dom'
- import './App.css';
- import Home from './components/Home';
- import ContactUs from './components/ContactUs';
- import NotFound from './components/notfound';
- function App() {
- return (
- <Router>
- <div>
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><Link to={'/'} className="nav-link"> Home </Link></li>
- <li><Link to={'/ContactUs'} className="nav-link">Contact Us</Link></li>
- </ul>
- </nav>
- <hr />
- <Switch>
- <Route exact path="/" component={Home} />
- <Route path="/contactus/:name" component={ContactUs} />
- <Route component={NotFound} />
- </Switch>
- </div>
- </Router>
- );
- }
-
- export default App;
Now in contactus.js, update the code as below,
- import React, { Component } from 'react'
-
- export class ContactUs extends Component {
- render() {
- console.log(this.props)
- return (
- <div>
- Contact Us Component
- </div>
- )
- }
- }
-
- export default ContactUs
The output will be displayed in the console,
Here in the console, we can see that this.prop has multiple objects so to access the properties from this.props.match.
- import React, { Component } from 'react'
-
- export class ContactUs extends Component {
- render() {
- const { params } = this.props.match
- return (
- <div>
- <p>{params.name}</p>
- Contact Us Component
- </div>
- )
- }
- }
-
- export default ContactUs
The output will be as below,
Implementing Nested Routes in React
Nested routes are used to implement sub-routes like contactus/Priyanka, contactus/test. For implementing nested routes, we need to import react-router component,
App.js
- import React from 'react';
- import { Route, Link, Switch, BrowserRouter as Router } from 'react-router-dom'
- import './App.css';
- import Home from './components/Home';
- import ContactUs from './components/ContactUs';
- import NotFound from './components/notfound';
- function App() {
- return (
- <Router>
- <div>
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><Link to={'/'} className="nav-link"> Home </Link></li>
- <li><Link to={'/ContactUs'} className="nav-link">Contact Us</Link></li>
- </ul>
- </nav>
- <hr />
- <Switch>
- <Route exact path="/" component={Home} />
- <Route path="/contactus" component={ContactUs} />
- <Route component={NotFound} />
- </Switch>
- </div>
- </Router>
- );
- }
-
- export default App;
Now in ContactUs.js component, we need to implement sub route in this component only,
- import React, { Component } from 'react'
- import { Route, Link } from 'react-router-dom';
-
- const Contact = ({ match }) => <p>{match.params.name}</p>
-
- export class ContactUs extends Component {
- render() {
- const { url } = this.props.match
- return (
- <div>
- <p>Contact Us Component</p>
- <strong>select contact name</strong>
- <ul>
- <li><Link to="/contactus/Priyanka">Priyanka</Link></li>
- <li><Link to="/contactus/test">Test</Link></li>
- </ul>
- <Route path="/contactus/:name" component={Contact}></Route>
- </div>
- )
- }
- }
-
- export default ContactUs
The output will be displayed as below,
In the above example, we are not getting a clue which link is clicked. So for that React provides a NavLink concept,
NavLink is used to style the active routes so that the user can identify which page he is user is navigating to.
Import NavLink from react-route-dom
Now replace all Link by NavLink component,
- import React from 'react';
- import { Route,NavLink, Switch, BrowserRouter as Router } from 'react-router-dom'
- import './App.css';
- import Home from './components/Home';
- import Contact from './components/ContactUs';
- import NotFound from './components/notfound';
- function App() {
- return (
- <Router>
- <div>
- <nav className="navbar navbar-expand-lg navbar-light bg-light">
- <ul className="navbar-nav mr-auto">
- <li><NavLink exact to={'/'} activeClassName="active" className="nav-link"> Home </NavLink></li>
- <li><NavLink to={'/contactus'} activeClassName="active" className="nav-link">Contact Us</NavLink></li>
- </ul>
- </nav>
- <hr />
- <Switch>
- <Route exact path="/" component={Home} />
- <Route path="/contactus" component={Contact} />
- <Route component={NotFound} />
- </Switch>
- </div>
- </Router>
- );
- }
-
- export default App;
contactus.js
- import React, { Component } from 'react'
- import { Route, NavLink } from 'react-router-dom';
-
- const Contact = ({ match }) => <p>{match.params.name}</p>
-
- export class ContactUs extends Component {
- render() {
- return (
- <div>
- <p>Contact Us Component</p>
- <strong>Select contact name</strong>
- <ul>
- <li><NavLink to="/contactus/Priyanka">Priyanka</NavLink></li>
- <li><NavLink to="/contactus/test">Test</NavLink></li>
- </ul>
- <Route path="/contactus/:name" component={Contact}></Route>
- </div>
- )
- }
- }
-
- export default ContactUs
The output will be displayed as below,
Summary
In this article, we have learned about Routing in React, how to implement the nested route and how to implement active link navigation, and the concept of the Switch. You can download the source code attached along with this article.