HI WELCOME TO SIRIS

Routing in React using React-Router: Part2

Leave a Comment
In our previous article, we have developed simple routing mechanism where we can traverse through different URLs of the application, the same way if we have tons of pages into the application so in this case, the situation will be more complex to handle.For that, we can develop child routing or you can say nested routing in react application. To implement child or nested routing, in this article we will learn a simple approach for that.
In this part of react router series, I have used few of the same package as we have used previously in React Routing Part1, so I am just going to modify those files to implement our nested routing functionality. In this article, I have used many react-bootstrap components which are listed below.
  • Navbar
  • Nav
  • Panel
  • So these are some primary components I have used to design a user interface, now let’s see the steps to configure nested routing.

Steps to configure nested/child routing

To implement nested or child routes, we need to follow few steps to implement the functionality.

Step 1

Previously, we have created three primary files named
Home.js
  1. import React from 'react';
  2. const Home = () => (
  3. <div>
  4. <h1>This Is From Dashboard Page</h1>
  5. </div>
  6. )
  7. export default Home;
About.js
  1. import React from 'react';
  2. const About = () => (
  3. <div>
  4. <h1>This Is From About Us Page</h1>
  5. </div>
  6. )
  7. export default About;
Contact.js
So in this file, we are actually implementing child or nested routes, open the file and paste below code snippet.
  1. import React from 'react';
  2. import Profile from './child/Profile';
  3. import Email from './child/Email';
  4. import Phone from './child/Phone';
  5. import { Tabs, Tab, Panel } from 'react-bootstrap';
  6. // To use routing functionalities
  7. import { Link, Switch, Route } from 'react-router-dom';
  8. const divStyle = {
  9. width: '50%',
  10. margin: '7px 27%'
  11. };
  12. const Contact = (match) => (
  13. <div>
  14. <h1>This is From Contact US Page</h1><hr />
  15. <Link to="/contact">Profile</Link> |
  16. <Link to="/contact/email">Email</Link> |
  17. <Link to="/contact/phone">Phone Number</Link>
  18. <Switch>
  19. <Route exact path='/contact' component={Profile} />
  20. <Route exact path='/contact/email' component={Email} />
  21. <Route exact path='/contact/phone' component={Phone} />
  22. </Switch>
  23. </div>
  24. )
  25. export default Contact;
As you can see that in contact us page, I have also included <link> to apply child components to be rendered and same as Switch will allow us to render appropriate user interface matching with the requested URL. Apart from these three files, we can create more file to see the functionality for a child or nested routing, for that create another three more files inside a new folder named Child.
/components/child/Profile.js
  1. import React from 'react';
  2. import { Tabs, Tab, Panel } from 'react-bootstrap';
  3. const divStyle = {
  4. width: '50%',
  5. margin: '7px 27%'
  6. };
  7. const Profile = () => (
  8. <div>
  9. <Panel style={divStyle}>
  10. Ubgifu79bg79 <Panel.Heading>Profile Page </Panel.Heading>
  11. <Panel.Body><a href="https://www.dotnettricks.com/mentor/manav">Click Here</a></Panel.Body>
  12. </Panel>
  13. </div>
  14. )
  15. export default Profile;
/components/child/Email.js
  1. import React from 'react';
  2. import { Tabs, Tab, Panel } from 'react-bootstrap';
  3. const divStyle = {
  4. width: '50%',
  5. margin: '7px 27%'
  6. };
  7. const Email = () => (
  8. <div>
  9. <Panel style={divStyle}>
  10. <Panel.Heading>Email</Panel.Heading>
  11. <Panel.Body>pandya.manav@gmail.com</Panel.Body>
  12. </Panel>
  13. </div>
  14. )
  15. export default Email;
/components/child/Phone.js
  1. import React from 'react';
  2. import { Tabs, Tab, Panel } from 'react-bootstrap';
  3. const divStyle = {
  4. width: '50%',
  5. margin: '7px 27%'
  6. };
  7. const Phone = () => (
  8. <div>
  9. <Panel style={divStyle}>
  10. <Panel.Heading>Contact</Panel.Heading>
  11. <Panel.Body>1234567891</Panel.Body>
  12. </Panel>
  13. </div>
  14. )
  15. export default Phone;
All above three of the file just containing static data, and as you can observe that here in these files, I have used Panel component of react-bootstrap.

Step 2

If you have followed previous part of this series then we have developed primary routing into an app.js file which is the main component for our web application, Apart from the routing part, as you can see that I have used additional components which is react-bootstrap. This is used to design our layout. Open app.js and paste the following code snippet.
App.js
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. // ract-bootstrap components
  4. import { Navbar, Nav, NavItem, MenuItem, NavDropdown , Tabs , Tab } from 'react-bootstrap';
  5. // Our speperately created components
  6. import Home from './Components/Home';
  7. import About from './Components/About';
  8. import Contact from './Components/Contact';
  9. // To use routing functionalities
  10. import { Link, Switch, Route } from 'react-router-dom';
  11. class App extends Component {
  12. render() {
  13. return (
  14. <div className="App">
  15. <Navbar>
  16. <Navbar.Header>
  17. <Navbar.Brand>
  18. <a>Routing With React-Router Part-2</a>
  19. </Navbar.Brand>
  20. </Navbar.Header>
  21. <Nav>
  22. <NavItem href="javascript:void(0)">
  23. <Link to="/">Dashboard</Link>
  24. </NavItem>
  25. <NavItem href="javascript:void(0)">
  26. <Link to="/about">About Us</Link>
  27. </NavItem>
  28. <NavItem href="javascript:void(0)">
  29. <Link to="/contact">Contact Us</Link>
  30. </NavItem>
  31. </Nav>
  32. </Navbar>
  33. <Switch>
  34. <Route exact path='/' component={Home} />
  35. <Route path='/about' component={About} />
  36. <Route path='/contact' component={Contact} />
  37. </Switch>
  38. </div>
  39. );
  40. }
  41. }
  42. export default App;

Step 3

So far, we have covered our user interface part, but let me clarify one thing that if you execute above example then it would not work because we have not implemented BrowserRouter to our main component which is index.js.

What is BrowserRouter

When we are working with the router in react, in the background it manages history API of HTML5, I mean when we routing through different pages that that time we should maintain history so that components will be available to route back again. Basically, it manages three different events which are listed below.
  • pushState
  • popState
  • replaceState
  • These events are used to manage the different routing activity using state and update accordingly based on the history object.
To configure BrowserRouter, we need to import the API from the package react-router-dom like this.
  1. import { BrowserRouter } from 'react-router-dom';
After the completion of the BrowserRouter configuration the final index.js file looks like this.
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import App from './App';
  5. import registerServiceWorker from './registerServiceWorker';
  6. import { BrowserRouter } from 'react-router-dom';
  7. ReactDOM.render(
  8. <BrowserRouter>
  9. <App />
  10. </BrowserRouter>, document.getElementById('root'));
  11. registerServiceWorker();

Note

Before executing the project, make sure to update index.html file like this.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <meta name="theme-color" content="#000000">
  7. <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
  8. <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  9. <!-- Bootstrap CDN Files -->
  10. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
  11. crossorigin="anonymous">
  12. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
  13. crossorigin="anonymous">
  14. <!-- End -->
  15. <title>React App</title>
  16. </head>
  17. <body>
  18. <noscript>
  19. You need to enable JavaScript to run this app.
  20. </noscript>
  21. <div id="root"></div>
  22. </body>
  23. </html>
Because it contains the two different style sheets which are responsible for all the themes related setting if you omit those style sheets than it is may possible that you cannot see the output in a proper manner.Our last step is to execute the application and see that how nested/child routing works.

Output

  • Dashboard Page
  • This page is the entry point of our application.
  • About Us Page
  • Contact US (With profile page as a child)
  • Here in this page, we have implemented child/nested routes, when we click on the contact link at that time it by default loads the profile page because we are told them to do so.
  • Email Details Page
  • When you click on the email link, at that time just observer the URL that how child key was added to the URL.
  • Phone Number Details Page
  • This is the contact us page where we have printed the contact number details.
If you look at all above pages, from the contact pages we have three separate child routes and whenever we click on appropriate links at that time it loads the only content of child and replaces with the existing one.This Is how we can achieve child/nested routing, but keep in mind that it does not follow any standard, it’s just only for practice purpose thus you can try and implement it by following the proper standard.
Summary
In this two-part of the series using react-router, we have covered so many concepts with bootstrap components, let’s recap it quickly.
  • Introduction to routing
  • Packages to use for routing
  • Steps to configure simple routing
  • Steps to configure child/nested routing
  • How to use bootstrap components with react
  • I hope you have learned something from these two part of series, I have shared the source code with this article and try to implement all the feature that we have covered and apply it to your project.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.