Add routing to your React Application using `@reach/router`

Sara Vieira
InstructorSara Vieira
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

In this lesson, we will be adding routing and two routes (homepage and about page) to a React application using the @reach/router library

Sara Vieira: [00:00] Let's import Router from Reach Router. I'm going to import, and it's not the default export, so import { Router } from "@reach/router."

[00:11] In order to use it, I'm going to create two different components, one for a home page and one for a fictitious About page. I'm going to say, const home page equals -- this is going to render in h1 -- saying home page, like this. Then, I'm going to do the exact same thing for the About page, so About and About.

[00:34] In order to use this, we need to first wrap a component around it. We say, router, and we close it. We don't allow React to do that. Another way we use it, is that we put the home page...For example, I put home page here and mount it, and then I give it a path, so where is this actually going to show.

[00:56] I say, path equals just this, like this, and I close it.

[01:02] This means that now, it will actually say home page. If I want to add an About I can do the same thing. I can say add slash about, and I can say About.

[01:13] You can't see anything now, because it renders the home page. If I go to About, it renders the About page. If I go back, it renders the home page. This is how you can add Reach Router to your React application.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today