-
Notifications
You must be signed in to change notification settings - Fork 0
React Router
kitiya edited this page Feb 3, 2020
·
4 revisions
To install the react router you need to download the react-router-dom package by running the following commands.
$ npm install react-router-dom
React router gives us three components [Route, Link, BrowserRouter] which help us to implement the routing.
From index.js file
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
const routing = (
<Router>
<Header />
<Switch>
<Route exact path="/" component={App} />
<Route path="/users" component={Users} />
<Route path="/contact" component={Contact} />
<Route component={Notfound} />
</Switch>
</Router>
)
ReactDOM.render(routing, document.getElementById('root'))In the Route component, we need to pass the two props
- path: it means we need to specify the path.
- component: which component user needs to see when they will navigate to that path.
const Header = (
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/users">Users</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</div>
);