React Router #
- Generally you wrap the top-level component of your app with
<Router>
- Anywhere in your app you can then have a
<Switch>
<Route path="/path">
<ComponentToRenderIfPathIsMatched />
</Router>
</Switch>
to match a path and render a compoonent
-
While there is a way to have a route configuration like in
vue-router
you generally do conditional rendering withSwitch
and nestedSwitch
statements -
To nest switches you put them in other components but still have to use the full path (e.g.
/accounts/add
) -
You route programmtically with a
<Redirect/>
component -
You can also use history.push(ā/ā)
Programmatic Navigation #
- You can route programmtically in a declaritive style with a
<Redirect/>
component - You can also use
history.push('<path>')
wherehistory
is a library thatreact-router
depends on- To use this, when initializing your Router, you must wrap it in a
<Router history={history}>
- For some reason this does not work with
browserRouter
, onlyRouter
and Iām not sure why
- To use this, when initializing your Router, you must wrap it in a