0

Disclaimer: I'm pretty sure this is a duplicate, because there's no way no one asked this, but I don't know if i searched the wrong terms or so but I couldn't find an answer, so I'll ask here.

I'm using React routers (react-router-dom), so for example if I set:

<Route path='/home' component={Home} />

At this point, obviously, if I go to localhost:3000/home it will load my homepage, right? But if I go to localhost:3000/fjnisodjuhfosij (just random text) it will load anyway a blank page, it won't say something like "this page doesn't exist" or just redirecting to the home. How can I handle this? Thank you

2 Answers 2

1

Just add Route with path '*':

<Route path='*' component={YourComponent} />
Sign up to request clarification or add additional context in comments.

Comments

1

To direct to home: Add this as the last route in switch: <Route><Redirect to="/home"/></Route>

Or, to show a 404 error page: Create a component for the error page (NotFound lets say) and add it as the last route: <Route component={NotFound} />

2 Comments

this works fine, but now i got a new problem, I set a route on / that redirects to home and a route that redirects to not found, but now every unknow route, just get redirected to home.
Could you share the whole <Router> part? You should useexact for the route /.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.