In this blog, I'm going to develop a small POC in React using React-Dom-Router. Routing becomes very important as an application grows, especially for handling the navigation from one component to another. I will be covering the basics using Stateful and Stateless components.

Follow the below npm commands.

  1. npm install -g create-react-app // To install react library for React development
  2. npx create-react-app my-app_Demo //create new react application
  3. npm install --save react-router-dom // router comes in different package

Let's import the route-related class.

  1. import { BrowserRouter as Router, NavLink, Route, Switch } from 'react-router-dom';
BrowserRouter
It's like a parent React component which keeps an eye on the URL changes.
  1. <Router>
  2. <NavLink>Your Code goes here</NavLink>
  3. <Route path="/blog" exact strict component={Blog} />
  4. lt;/Router>

Route

Based on the URL passed by the parent, it will render other component as per as its matching component.
  1. <Route path="/blog" exact strict component={Blog} />
NavLink
It's just a new version of <Link>. To use an active route class, we commonly use NavLink.
  1. <NavLink to="/about" className="nav-link" exact activeStyle={{ color: "black" }} >

It's a simple navigation button; on click of this link, it will redirect to the "/about" component.

exact
This means the URL should be "localhost:3000/about". If you don't put the exact one, it can throw an error. If http://localhost:3000/ is used, it will match "/" and render the Home component as well.
Note
Use exact only when there are no nested routes for the specified path. Here's the complete code.
  1. import React, { Component } from "react";
  2. import "./App.css";
  3. import { BrowserRouter as Router, NavLink, Route , Switch } from "react-router-dom";
  4. import Contact from "./Contact";
  5. import Blog from "./blogs";
  6. import App2 from './RouterPara';
  7. import UrlPara from './UrlPara';
  8. class App extends Component {
  9. render() {
  10. return (
  11. <Router>
  12. <div className="App">
  13. <nav className="navbar navbar-expand-lg navbar-light bg-light navbar-static-top">
  14. {/* <a className="navbar-brand" href="https://rajendrataradale.wordpress.com/author/rajendrataradale/">
  15. RT's Knowledge World
  16. </a> */}
  17. <NavLink to="/" className="navbar-brand" exact activeStyle={{ color: "black" }}>
  18. RT's Knowledge World
  19. </NavLink>
  20. <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  21. <span className="navbar-toggler-icon" />
  22. </button>
  23. <div className="collapse navbar-collapse" id="navbarNav">
  24. <ul className="navbar-nav">
  25. <li className="nav-item active">
  26. <NavLink to="/" className="nav-link" exact activeStyle={{ color: "black" }}>
  27. Home
  28. </NavLink>
  29. </li>
  30. <li className="nav-item">
  31. <NavLink to="/about" className="nav-link" exact activeStyle={{ color: "black" }}>
  32. About
  33. </NavLink>
  34. </li>
  35. <li className="nav-item">
  36. <NavLink to="/blog" className="nav-link" exact activeStyle={{ color: "black" }}>
  37. Blogs
  38. </NavLink>
  39. </li>
  40. <li className="nav-item">
  41. <NavLink to="/contact" className="nav-link" exact activeStyle={{ color: "black" }}>
  42. Site Admin
  43. </NavLink>
  44. </li>
  45. <li className="nav-item">
  46. <NavLink to="/data" className="nav-link" exact activeStyle={{ color: "black" }}>
  47. Test Data
  48. </NavLink>
  49. </li>
  50. <li className="nav-item">
  51. <NavLink to="/urlpara/255066" className="nav-link" exact activeStyle= {{ color: "black" }}>
  52. Test URL Parameters
  53. </NavLink>
  54. </li>
  55. </ul>
  56. </div>
  57. </nav>
  58. <hr className="my-4" />
  59. <div>
  60. <Switch>
  61. <Route path="/" exact strict render={()=> { return (
  62. <div className="jumbotron container row">
  63. <h1 className="display-4">Welcome Home!</h1>
  64. <p className="lead">
  65. Unless you try to do something beyond what you have already mastered, you will never grow
  66. </p>
  67. <hr className="my-4" />
  68. <p>
  69. Sometimes it’s necessary to go a long distance out of the way in order to come back a short distance correctly.
  70. </p>
  71. <br />
  72. <a className="btn btn-primary btn-lg" href="https://rajendrataradale.wordpress.com/author/rajendrataradale/" role="button">
  73. Learn more
  74. </a>
  75. </div>
  76. ); }} />
  77. <Route path="/about" exact strict render={()=> { return (
  78. <div className="card">
  79. <div className="card-header">Rajendra Taradale</div>
  80. <div className="card-body">
  81. <blockquote className="blockquote mb-0">
  82. <p>
  83. Over 8 Years of experience in IT Industry. Hands-on experience in all phases of Software Development Life Cycle (SDLC) in Web and Windows Applications under Agile Framework (Scrum, Kanban, and SAFe-Scaled)
  84. </p>
  85. <footer className="blockquote-footer">
  86. Welcome
  87. <cite title="Source Title">
  88. RT's Knowledge World
  89. </cite>
  90. </footer>
  91. </blockquote>
  92. </div>
  93. </div>
  94. ); }} />
  95. <Route path="/blog" exact strict component={Blog} />
  96. <Route path="/contact" exact strict component={Contact} />
  97. <Route path="/data" exact strict component={App2} />
  98. <Route path="/urlpara/:id" exact strict component={UrlPara} />
  99. </Switch>
  100. </div>
  101. </div>
  102. </Router>
  103. );
  104. }
  105. }
  106. export default App;

match: will help you to read the URL parameters.

  1. <Router>
  2. <NavLink to="/urlpara/255066" className="nav-link" exact>Test URL Parameters</NavLink>
  3. <Route path="/urlpara/:id" exact strict component={UrlPara} />
  4. </Router>

The component code goes as below for reading the URL parameters.

  1. import React, { Component } from 'react';
  2. class UrlPara extends Component {
  3. render() {
  4. return (
  5. <div>
  6. User Navigated to : {this.props.match.params.id}
  7. <p>
  8. match contains : ${JSON.stringify(this.props.match,null,4)}
  9. </p>
  10. </div>
  11. );
  12. }
  13. }
  14. export default UrlPara;

Let's see the output in the browser.

Understanding React Routing with Sample Project
Switch()
Also, we need to make sure that only one component renders at a time.
  1. <Switch>
  2. <Route path="/blog" exact strict component={Blog} />
  3. <Route path="/contact" exact strict component={Contact} />
  4. </Switch>
Understanding React Routing with Sample Project
Above is the complete demo POC with a highlighted section of React routing concepts.
Complete code reference to get started