npm i react-router-dom
.Route: It is the main component of React router dom. It is used to render the content based on the pathname. To use it you need to import react from react-router-dom. It has mainly to props i.e
Switch: When we use multiple routes together then react will render all the route components with the matched path to avoid this we use all the route components under the switch component and the switch will only render the first element with the matching path. If more than two-element have similar paths then we use “ exact” keyword to find the exact match of the link.
Example:
function Roster() {
return (
<div>
<h2>This is a roster page!</h2>
<Switch>
<Route exact path='/roster' component={FullRoster}/>
<Route path='/roster/:number' component={Player}/>
</Switch>
</div>
);
}
Link : This component is similar to the anchor tag in the HTML except anchor reloads the page but link component do the client site rendering and don’t reloads the page that’s why it is much faster. It helps us navigate between pages without reloading. It has a prop “To” which excepts the path to which user want to navigate.
Example
import { Link } from 'react-router-dom'
function Header() {
return (
<header>
<nav>
<ul>
<li><Link to='/'>Home</Link></li>
<li><Link to='/roster'>Roster</Link></li>
<li><Link to='/schedule'>Schedule</Link></li>
</ul>
</nav>
</header>
);
}
Nav Link: It’s the same as Link except it has an extra prop i.e “activeClassName” in it we can give a class name and give css property to it and many more.