React Router
React Router is the most used routing solution for React.
React Router Installation
1 | npm install react-router-dom |
First we need to wrap everything that gets rendered inside <BrowserRouter>
element. we can do that in index.js
1 | import {BrowserRouter} from "react-router-dom"; |
A <Switch>
looks through all its children <Route>
elements and renders the first one whose path matches the current URL. Use a <Switch>
any time you have multiple routes, but you want only one of them to render at a time
Here the component will be display only when the path matches the current url. The exact
attribute means the URL has to be exact match. Without the exact
attribute, other URLs that contains “/“ is a match.
1 | import {Switch, Route, Link} from "react-router-dom"; |
Here is Home, About and Dashboard components
1 | function Home() { |
You can add navigation links to help navigate the URLs
1 | <ul> |
To try out the basic example: https://reactrouter.com/web/example/basic
Reference