Your experience on this site will be improved by allowing cookies
The process of directing a user to different pages based on their action or request is known as routing. In ReactJS the process of routing is used for developing Single Page Web Applications, mainly to define multiple routes in the application. The ReactJS provides a standard library system to create routing in the React application using React Router Package.
Importance of React Router:
Advantages Of React Router:
React Router Installation:
There are three different packages for routing in ReactJS.
The react-router-dom modules need to be installed in the user’s application, to use react routing, as it is not possible to install react-router directly.
Command:
$ npm install react-router-dom --save
Components in React Router:
There are two types of components in the React router.
Example: Routing Step 1:
Blog.js:
import React from 'react' class Blog extends React.Component { render() { return Blog} } export default Blog; |
Services.js:
import React from 'react' class Services extends React.Component { render() { return Services} } export default Services; |
App.js:
import React from 'react' class App extends React.Component { render() { return ( Home |
Explanation:
Here, two components are created along with the already present App.js.
Route:
To define and render component based on the specified path, Route is used.
Routing Step 2:
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { Route, Link, BrowserRouter as Router } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' const routing = ( |
Explanation:
For Routing, the process will then flow as; opening the index.js file; importing all the three component files in it; importing line: import { Route, Link, BrowserRouter as Router } from ‘react-router-dom’ to implement the Routing.
Routing Step 3:
● Open the command prompt.
● Go to the project location.
● Type npm start.
Output:
● Enter localhost:3000/blog, manually in the browser.
Output:
Routing Step 4:
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { Route, Link, BrowserRouter as Router } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' const routing = ( |
Output:
Explanation:
To avoid the rendering of the Home component, the exact prop is used.
Adding Navigation using Link component:
To create links which allow navigating on different URLs component is used. It allows the rendering of the links’ content without reloading the webpage. In simple words, to allow multiple linking on a single page, component needs to be imported in the index.js file.
Example:
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { Route, Link, BrowserRouter as Router } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' const routing = ( |
Output 1: When active link is Home.
Output 2: When active link is Blog.
Explanation:
Routes are rendered on the screen after adding Link.
The component:
To add style to the active routes, the NavLink component is used.
Example:
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Route, Link, NavLink } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' const routing = ( |
Output 1: When active link is Home.
Output 2: When active link is Blog.
Output 3: When active link is Services.
Explanation:
Here, we are adding some styles to the Link. Home link is of the colour green when it is the only currently active link. Blog link is of colour blue when it is the currently active link. Services link is of colour crimson when it is the currently active link.
React Router Switch:
To render components only when the path matches, the component is used.
Example:
notfound.js:
import React from 'react' const Notfound = () => Does not exist.export default Notfound |
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Route, Link, NavLink, Switch } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' import Notfound from './notfound' const routing = ( |
Output:
Explanation:
The not found error will be returned if we manually enter the wrong path.
React Router component:
To redirect to another route in an application, the component is used. It is used to maintain the old URLs and can be placed anywhere in the route hierarchy.
Nested Routing in React:
To render sub-routes in a ReactJS application, nested routing is done.
Example:
Index.js:
import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter as Router, Route, Link, NavLink, Switch } from 'react-router-dom' import './index.css'; import App from './App'; import Blog from './blog' import Services from './services' import Notfound from './notfound' const routing = ( |
Services.js:
import React from 'react' import { Route, Link } from 'react-router-dom' const Services = ({ match }) => {match.params.id} class Services extends React.Component { render() { const { url } = this.props.match return (We are checking for the Services offered.Select any Service.
|
Output 1: Before clicking on the Services link.
Output 2: After clicking on the Services link.
Explanation:
Here, we are importing the React Router component in the services.js file to implement the subroutes.
0 comments