Routing in React
- It is often useful to refer to specific “pages”/objects in an app with
a specific URL.
- Demonstrate “tab” behavior of ColorList3.
- This is the “default” behavior of a traditional app.
- Need to use a library to do this in React.
- Add
react-router
and react-router-dom
to npm project.
- Surround root component with
<Router>
tag.
- show changes to
index.js
- Note
BrowserRouter as Router
- I’m not sure why authors did this instead of just use
BrowserRouter
.
Perhaps just to demonstrate a use of that syntax.
- Add routes to the application. (Can be anywhere in application.)
- Demonstrate entering URL by hand.
- Look at default/wildcard route.
- Look at
NavBar
in Tabs.js
- You can nest routes
- You can set up routes to behave like resources (i.e., take part of the URL as a parameter.)
- uncomment the
:id
route in App.js