DEV Community

Cover image for How to set the default route in ReactJS?

Posted on • Originally published at

How to set the default route in ReactJS?

This article was originally published at

Some time ago, we had an opportunity to use the component named “DefaultRoute” in the react routing.

Now, its depreciated method, and it’s not so popular to use it, you can create the custom route named default or whatever, but still, it’s not how we do it in modern React.js development.

It’s just because using the “DefaultRoute” route, we can cause some rendering problems, and its the thing that we definitely would like to avoid.

But don’t worry!

We have a solution to that problem, and I’d say its much more comfortable, but what more important, its a good practice.

To handle the default routing now, we can use two methods, one of them will be “*“, like routing, and the second one is “/”, that will redirect us to the defined component on the “default” path.

I’d recommend going with the “/” and set up the 404 component for the non-found pages instead of auto-redirect to the “default”.

Let’s see on the code example how it can be created.

  <Route path="/about">
    <About />
  <Route path="/contact/:id">
    <Contact />
  <Route path="/contact">
    <AllContacts />
  {/* The default route */}
  <Route path="/">
    <Home />

Duomly - Programming Online Courses

Thank you for reading,
Radek from Duomly

Top comments (0)