Skip to content

Instantly share code, notes, and snippets.

@Tristinsorrells1
Forked from letakeane/React_Router_prework.md
Last active February 6, 2023 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tristinsorrells1/82f3c53405be36f73ccf271c9673cf26 to your computer and use it in GitHub Desktop.
Save Tristinsorrells1/82f3c53405be36f73ccf271c9673cf26 to your computer and use it in GitHub Desktop.

Questions / Readings

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?

Node / npm

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.

  1. What is a <BrowserRouter />?

A Router that uses the HTML5 history API (pushState, replaceState and the popstate event) to keep your UI in sync with the URL.

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

BrowserRouter is used for doing client side routing with URL segments.

Note:

To use a router, make sure it is rendered at the root of your element hierarchy. Typically you’ll wrap your top-level element in a router

Route Matchers (

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

Any component passed as a child to is called a “Route Component”. Its most basic responsibility is to render some UI when its path matches the current URL. One important thing to note is that a matches the beginning of the URL, not the whole thing. So a will always match the URL. Because of this, we typically put this last in our . Another possible solution is to use which does match the entire URL.

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

A route's component is rendered when that route matches the URL. It will be rendered with route props.

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

When a is rendered, it searches through its children elements to find one whose path matches the current URL. When it finds one, it renders the first child or that matches the location and ignores all others. This means that you should put s with more specific (typically longer) paths before less-specific ones.

  1. How does it decide what to render?

The Switch component will work much in the same way as the Router component, but it will only render the first matched child.

Route Changers (aka navigation)

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

React Router provides a component to create links in your application. Wherever you render a , an anchor () will be rendered in your HTML document.

  1. 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.

  1. 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.

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