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.
- Fork this gist
- On your own copy, go through the listed readings and answer associated questions
- Comment a link to your forked copy on the original gist
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:
-
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)
-
What package do we need to install to use React Router? After running create-react-app and cd-ing into the directory, run npm install react-router-dom
React Router provides a series of helpful components that allow our apps to use routing. These can be split into roughly 3 categories:
- Routers (
<BrowserRouter>
,<HashRouter>
) - Route Matcher (
<Route>
,<Switch>
) - Route Changers (
<Link>
,<NavLink>
,<Redirect>
)
All components must be imported explicitly from react-router-dom
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.
-
What is a
<BrowserRouter />
? A type of<Router>
component that uses the HTML5 history API. -
Why would we use
<BrowserRouter />
in our apps? Keeps UI and URL in sync with each other.
-
What does the
<Route />
component do? Renders UI when its path matches the current URL. -
How does the
<Route />
component check whether it should render something? It looks for the specified 'exact path' to know when something should or shouldn't be rendered based on the URL. -
What does the
<Switch />
component do? Renders the first child<Route>
or<Redirect>
that matches the location. Used when you want to route exclusively. -
How does it decide what to render? Depending on its children, the switch will look at the 'path' prop (
<Route>
) or the 'from' prop (<Redirect>
). Either way it will always choose the first child matching the location.
-
What does the
<Link />
component do? How does a user interact with it?<Link>
provides a declarative, accessible navigation around an application. A user clicks on an element wrapped in a<Link>
component and will be taken to the corresponding path. -
What does the
<NavLink />
component do? How does a user interact with it? A version of<Link>
that adds style attributes to the rendered element. User clicks on an element wrapped in the component. -
What does the
<Redirect />
component do?<Redirect>
will navigate to a new location that overrides the current location in the history stack.