The course is part of this learning path
This module looks at how to Route in React. You’ll be looking at linking components, creating dynamic links, parameterized routes, and how to install the React Router DOM.
The objectives of this module are to provide you with an understanding of:
- How to install the React Router DOM
- Hooks in React
- How to create restful services
- How to use an Effect Dependency Array
- How to hand errors in data requests
- How to send data
This learning path is aimed at all who wish to learn how to use the ReactJS framework.
We welcome all feedback and suggestions - please contact us at email@example.com to let us know what you think.
For our app component, we've now added three Route components. The one on line 24 defines the Route for the path for the search link. We use the exact attribute here, so that this path only matches when there are no other segments in the URL. This Route renders the FilterableCoursesTable component when the path and the location path name match. The Route on line 24 has a path of /add, and this will render the AddCourseForm component when the location path name is /add. The Route on line 35 has no path property. This will render if no other Route matches. Inside this, we use React Router DOM's Redirect component. Used like this, it simply forwards the location to the value specified in the to attribute. In this case, to /. You can read more about this component and it's options in the React Router DOM documentation. You can see that we have surrounded the Route components in a Switch component. The Switch component is very useful as it ensures that React renders the first Route or Redirect that matches the location. If we didn't have the Switch component around our Routes, any Route that matched would render. Again, you can read more about the Switch component in the documentation. Clicking on each of the Route displays the views as specified. Clicking Add and Search display as we would want. The Unmatchable Route link, as you can see in the code, tries to go to a location that we don't have a Route for. In this case, the Redirect is activated, and the user is taken to the /Route and the FilterableCoursesTable is rendered. So, now we are able to render different views in the app without having to make requests for new pages, but there is more we can do with routing like rendering views with particular data in them depending on what the user has clicked.