Skip to content
Advertisement

React router always showing NotFound component

I have routes objects from backend and set it to routes like this and when I am set NotFound component, which route is ‘*’ to default in switch/case or to case “NotFound” which commented now, it all the time show with all components after them.

I mean it works all the time, not just in the wrong URL way

JavaScript

I’ve already tried a lot.. Even delete not-found route from backend object, and set it manually to Router like this

JavaScript

But in this way at totally not work

Any ideas? it shows all the time

Advertisement

Answer

That occurs because all children of a <Switch> should be <Route> or <Redirect> elements. You can check more about it in react-router-dom docs.

So, one solution for your code would be do something like that:

JavaScript
Advertisement