Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bruce-gordon/43456024d25cc5887d3930f40086bc8d to your computer and use it in GitHub Desktop.
Save bruce-gordon/43456024d25cc5887d3930f40086bc8d to your computer and use it in GitHub Desktop.

React Router Prework

This gist contains a short assignment I'd like everyone to complete before our formal lesson. The prework involves reading some of the React Router documentation, and will allow us to keep the lesson more hands on.

Instructions

  1. Fork this gist
  2. On your own copy, go through the listed readings and answer associated questions
  3. Comment a link to your forked copy on the original gist

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?

npm install 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.

  1. What is a <BrowserRouter />?

*A router that uses html5 history API to keep the US in synch with the url. It uses regular URL paths. *

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

This makes sure that the url changes to reflect which componenets or views are being displayed. There is a base url (basename: string) that reflects the home page or main view. When a user event causes the browser to change "pages" the url will change to reflect the new page (view, component, etc)

Route Matchers

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

has a property that contains a string for the end of the desired URL. Route will also have a nested component. When is looking for a particular url path it will look at each and when it finds one with a property matching the intended path then that will render its nested component ( for example).

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

If the path property for that component matches the current URL then it will render.

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

has child Router components nested inside of it and when it is rendered it will look through those child componenents to figure out which one to render.

  1. How does it decide what to render?

When renders it searches through its Route child components until it finds one with the matching path and then tells it to render. It it does not find a match then it will render the Route component with path="/" which is the baseline component. This will always be the last component nested because it is the least specific and would therefore match any url path. The more specific the path, the higher up it is nested.

Route Changers

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

The Link component adds a link to the app. It is similar (or equivalent to) adding an tag in html. has a "to" property with a value of the url path it will take the user to. The user would click the link to interact with it.

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

NavLink is a special version of Link that can style itself as "active". It does this if the "to" prop matches the current location. If the user is on the part of the page that the NavLink would take them to then that link is active.

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

<Redirect /> is for forcing navigation. You can render a Redirect to force the user to navigate to the path in the Redirect "to" prop. It's basically a way to force a user to a specific part of the app.

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