TIL About React Router

What follows is notes from a class. Incomplete thoughts & code that probably will only make sense to me. Given enough time, even that may be a stretch! Reference at your own risk 😂

React Router

Learning about react-router-dom.

There was a new way to do routing compared to a previous project I did where we used a switch statement for routing. Instead you can load a component into a Route:

<Route url="/cars" component={<CarsPage />}>
<Route url="/cars/add" component={<AddACarPage />}>

This solves problems if you have a lot of routes, especially if there are sub-routes like the above, because the switch statement will stop when it hits the first matching segment, regardless of what comes after it; this means you don’t have to keep track of which routes need exact path vs. just path.

The React router training is put together really well to learn the basics and not-so-basics.


I didn’t know there is a way to do routing in React without using React Router. But apparently it’s possible in the HTML5 spec! You can use [`history.pushState()](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to update what’s in the browser’s URL bar and history.