Notes

React Router Introduction

Getting started with routing

Creating frontend routes

const Root = () => {
  const users = {
    1: { name: "Andrew" },
    2: { name: "Raymond" },
  };

  return (
    <BrowserRouter>
      <div>
        <h1>Hi, I'm Root!</h1>
        <Route exact path="/" component={App} />
        <Route path="/hello" render={() => <h1>Hello!</h1>} />
        <Route path="/users" render={() => <Users users={users} />} />
      </div>
    </BrowserRouter>
  );
};

Route path params

<Route
  path="/users/:userId"
  render={(props) => <Profile users={users} {...props} />}
/>

React Router Navigation

Adding links for navigation

import { BrowserRouter, Route, Link } from "react-router-dom";

Switching between routes

<Switch>
  <Route path="some/url" component={SomeComponent} />
  <Route path="some/other/url" component={OtherComponent} />
  <Route component={DefaultComponent} />
</Switch>
<Route
  exact
  path="/"
  render={() => (this.props.currentUser ? <Home /> : <Redirect to="/login" />)}
/>

History

// Pushing a new URL (and adding to the end of history stack):
const handleClick = () => this.props.history.push("/some/url");

// Replacing the current URL (won't be tracked in history stack):
const redirect = () => this.props.history.replace("/some/other/url");

Nested Routes

Why nested routes?

What are nested routes?

const Profile = (props) => {
  // Custom call to database to fetch a user by a user ID.
  const user = fetchUser(props.match.params.userId);
  const { name, id } = user;

  return (
    <div>
      <h1>Welcome to the profile of {name}!</h1>

      <Link to={`/users/${id}/posts`}>{name}'s Posts</Link>
      <Link to={`/users/${id}/photos`}>{name}'s Photos</Link>

      <Route path="/users/:userId/posts" component={UserPosts} />
      <Route path="/users/:userId/photos" component={UserPhotos} />
    </div>
  );
};

Alt. version using props.match

// Destructure `match` prop
const Profile = ({ match: { url, path, params }) => {

  // Custom call to database to fetch a user by a user ID.
  const user = fetchUser(params.userId);
  const { name, id } = user;

  return (
    <div>
      <h1>Welcome to the profile of {name}!</h1>

      <Link to={`${url}/posts`}>{name}'s Posts</Link>
      <Link to={`${url}/photos`}>{name}'s Photos</Link>

      <Route path={`${path}/posts`} component={UserPosts} />
      <Route path={`${path}/photos`} component={UserPhotos} />
    </div>}
  );
};

React Builds

Reviewing common terminology

Configuration or code?

Babel and webpack (yes, that’s intentionally a lowercase ‘w’)

The Create React App build process

Ejecting

Preparing to deploy a React application for production

REACT_APP_FOO: some value
REACT_APP_BAR: another value
console.log(process.env.REACT_APP_FOO);
Can be referenced in your index.html like so: <title>%REACT_APP_BAR%</title>

Configuring the supported browsers

{
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Creating a production build