Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save demaceo/1f0a4f3c14c99e6af523bf0ba612de5f to your computer and use it in GitHub Desktop.
Save demaceo/1f0a4f3c14c99e6af523bf0ba612de5f 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.

3. What is a <BrowserRouter />?

  • a router that keeps the UI in sync with the URL.

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

  • basename (string): represents the main URL for all potential locations within the app.

  • <BrowserRouter basename="/calendar> <Link to="/today" /> </BrowserRouter> renders <a href="/calendar/today">.

  • foreRefresh (boolean): if true, full page refreshes occur when navigating pages in the app.

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

  • To make the UX more sensical/convenient (by adding URL points of reference) while navigating various pages throughout the site.

Route Matchers

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

  • provides a path to be rendered if the current URL matches it

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

  • if the URL matches whats written in the path.

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

  • When rendered, it searches through its children elements to find one whose path matches the current URL.
  • <Route path="/"> will always match the URL because all URLs begin with a /. So if none of the previous routes before it renders anything, then this route will act as a fallback and thus should be put last in the order.

8. How does it decide what to render?

  • Once a <Route path="/?? "> has been matched with the current URL, then that <Route path= "/?? "> is then rendered while ignoring all others.
  • If nothing is found, the <Switch> renders nothing (null)

Route Changers

9. What does the <Link /> component do? How does a user interact with it? Creates links in the application by rendering an anchor (<a>) tag in the HTML document.

  • Ex. <Link to="/">Home</Link> renders as <a href="/">Home</a>.

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

  • a special type of <Link> that can style itself as “active” when its to prop matches the current location. Ex. <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?

  • When rendered, <Redirect> will force navigate to whatever its to prop is.

  • Ex. <Redirect to="/login" />

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