Languages
[Edit]
EN

React / react-router 6 - basic routing example

13 points
Created by:
Aston-Freeman
787

In this article, we would like to show you a basic routing example in React.

To use routing in React, the first thing you need to do is install react-router-dom@6 with command line inside your project.

Then you have to import:

  • BrowserRouter (e.g. as Router) - that contains core routing logic,
  • Routes - that searches through its children (<Route> elements) to find the one whose path matches the current URL,
  • Route - to render some components when its path matches the current URL,
  • Link - to navigate through the routes.

By using the following code:

In below example we create three pages handled by the react-router:

  • Home page,
  • Gallery page,
  • About page.

As we click on the different <Link> components, the router renders the matching <Route> components.

Simple routing example:

References

Edit
  1. https://reactrouter.com/docs/en/v6/getting-started/overview 

Alternative titles

  1. React / react-router v6 - basic routing example
  2. React - redirect to another route using react-router 6
  3. React - redirect to another route using react-router v6
  4. React - react-router 6 example
  5. React - react-router v6 example
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join