React Router v5¶
[[React Router v6]]
Why React Router?¶
- client side routing- go to another page
- render a page given a URL
 
- also do server side rendering (with Remix or Next.js)
Philosophy and Intro¶
uidotdev: React Router v5: Philosophy and Introduction
What is BrowserRouter?¶
- Parent component
- can pass routing props to any component- uses React Context
 
<Route>¶
Route always has to render something
- children or render prop if there's a match
- nullif no match
import {BrowserRouter as Router} from 'react-router-dom'
ReactDOM.render(
    <Router>
        <App>
    </Router>
, document.getElementById('app'))
<Route> and partial matching¶
/users/new
- will render both UsersandNewUser
Route's render prop¶
<Route
    path="/path"
    render={({ match }) => (
      <React.Suspense fallback={<>...</>}>
        <Component id={parseInt(match.params?.id, 10)} />
      </React.Suspense>
    )}
  />,
Optional param¶
How to Add Optional Path Parameter with React Router v5
<Route exact path="/route1/" component={MyComp} />,
<Route exact path="/route1/:optionalParam" component={MyComp} />,
Switch¶
render the first <Route> that matches
let routes = (
  <Switch>
    <Route exact path="/">
      <Home />
    </Route>
    <Route path="/about">
      <About />
    </Route>
    <Route path="/:user">
      <User />
    </Route>
    <Route>
      <NoMatch />
    </Route>
  </Switch>
);
Protected Routes¶
Protected routes and authentication with React Router v5
You can create your own Routes
function PrivateRoute({ children, ...rest }) {
  return (
    <Route
      {...rest}
      render={() => {
        return fakeAuth.isAuthenticated === true ? (
          children
        ) : (
          <Redirect to="/login" />
        );
      }}
    />
  );
}
Prompt¶
Raise an alert when they try to leave
<Prompt
    when={hasUnsavedChanges()}
    message="You have unsaved changes, are you sure you want to leave?"
/>
Hooks¶
Why withRouter HOC?¶
Before withRouter¶
- You passed in a component to Route
- Routewould create a HOC and pass props- history,- match,- locationto your component 3. so you can- history.push("new/path")
Some components (example: Header) are in every page
- can't wrap with a Route
gives <Header /> access to the props
- history
- match
- location
Now they have their own hooks!
Why isn't history.push() changing the page?¶
we don't have a router wrapped around the component?
How to access history?¶
useHistory¶
Route's render prop
withRouter
Preventing transitions¶
https://fullchee-reminders.netlify.app/link/2070
Relative URLs¶
/configurator/1/.. --> 404
- theory: would resolve to /configurator
- actual: just checks for /configurator/1/..- it doesn't do a path.resolve
 
- it doesn't do a 
- need to route to /configuratorwithexact
I think it should be supported tho?
Link¶
Internal links only 😢
External links need to use <a>
We had a <Link> component that would use <Link /> or <a>  if the href was external