Let's learn about REACT ROUTER
Let us learn react-routing
Create React App
Firstly, Make a folder any where you wish to in your system
Go to the folder in the terminal and type the below command,to create react application
npx create-react-app your-app
NOTE: Make sure you have npm package intsalled already .If not install npm first.
You can name your react app what ever you wish to in place of your-app
And then type the command below to go into your-app folder
cd your-app
why we use React-Router
React Router is the standard routing library for React. It keeps your UI in sync with the URL and allows us to build a single-page web application with navigation without the page refreshing as the user navigates.
You can install React Router from the public npm registry with either npm or yarn.
Type the below code in your terminal
npm install react-router-dom
Three primary categories of components in React Router
routers like BrowserRouter and HashRouter
route matchers like Route and Switch
navigation routers like Link,NavLink and Redirect
However we will not deep dive into all of these for now, but let us see how we import them using below code snippet
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
we can use all these according to our necessity.
I will give you an example code
routing components use these in your file app.js
export default function App( ){
return(
<Router>
<div>
<nav> <ul>
<li><Link to ="/">Home</Link></li>
<li><Link to ="/about">About</Link><li>
<li><Link to ="/users">Users</Link><li>
</ul></nav>
<Switch>
<Route path ="/about">About/></Route>
<Route path ="/users">Users/></Route>
<Route path ="/">Home/></Route>
</Switch>
</div>
</Router>
);
}
Now that you have written the above code you can navigate through different links.
Do give it a try and Check the output
Hope this helped you!
Thankyou for reading!
Happy learning!