Skip to content

Instantly share code, notes, and snippets.

@carlymclift
Forked from khalidwilliams/React Router Prework.md
Last active August 25, 2020 15:03
Show Gist options
  • Save carlymclift/9152c506340db9051dda1dadc4fb8c4e to your computer and use it in GitHub Desktop.
Save carlymclift/9152c506340db9051dda1dadc4fb8c4e to your computer and use it in GitHub Desktop.

React Router Prework

Router Overview

React Router is a library that allows us to make our single page React applications mimic the behavior of multipage apps. It provides the ability to use browser history, allowing users to navigate with forward / back buttons and bookmark links to specific views of the app. Most modern sites use some form of routing. React Router exposes this functionality through a series of components. Let's start by looking at the overall structure of an app using router:

1. Take a look at the quick start page of the React Router docs. Take note of the syntax and organization of the page. No worries if this looks unclear right now! (nothing to answer here)

2. What package do we need to install to use React Router?

React Router DOM
npm install react-router-dom

  • At the top of the file import react router DOM:
import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";

Router Components

React Router provides a series of helpful components that allow our apps to use routing. These can be split into roughly 3 categories:

  • Routers
  • Route Matcher
  • Route Changers

Routers

Any code that uses a React-Router-provided component must be wrapped in a router component. There are lots of router components we can use, but we'll focus on one in particular. Let's look into the docs to learn more.

3. What is a <BrowserRouter />?

It is a <Router> that uses the HTML5 history API to keep your UI in sync with the URL.

  • pushState
  • replaceState
  • the popstate event

All of the components that you use in a web application should be imported from react-router-dom:

<BrowserRouter
  basename={optionalString}
  forceRefresh={optionalBool}
  getUserConfirmation={optionalFunc}
  keyLength={optionalNumber}
>
  <App />
</BrowserRouter>

4. Why would we use <BrowserRouter /> in our apps?

A <BrowserRouter> uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly.

  • Users can use urls to bookmark pages
  • Users can use the back or forward button
  • Users can easily share content from a page in the app

Route Matchers

5. What does the <Route /> component do?

Render some UI when a location matches the route’s path.

6. How does the <Route /> component check whether it should render something?

When a location matches the route’s path.

7. What does the <Switch /> component do?

Switch Renders the first child or that matches the location. is unique in that it renders a route exclusively (only one route wins).

8. How does it decide what to render?

When a <Switch> is rendered, it searches through its children <Route> elements to find one whose path matches the current URL. When it finds one, it renders that <Route> and ignores all others

  • This means that you should put s with more specific (typically longer) paths before less-specific ones.

Route Changers

9. What does the <Link /> component do? How does a user interact with it?

<Link> component creates links in your application. Wherever you render a <Link>, an anchor <a> will be rendered in your HTML document. User can interact by clicking on it.

<Link to="/">Home</Link>
// <a href="/">Home</a>

10. What does the <NavLink /> component do? How does a user interact with it?

The is a special type of that can style itself as “active” when its to prop matches the current location. User can click on it.

<NavLink to="/react" activeClassName="hurray">
  React
</NavLink>

// When the URL is /react, this renders:
// <a href="/react" className="hurray">React</a>

// When it's something else:
// <a href="/react">React</a>

11. What does the <Redirect /> component do?

Any time that you want to force navigation, you can render a . When a renders, it will navigate using its to prop.
<Redirect to="/login" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment