1

I can't seem to get my NotFound component to render. Whenever I type in some random characters in the url (ex: 'localhost:3000/asdfasdfasdfasdf'), the browser actually directs to my Topics component, with no content.

Here is how I have the routes set up:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

render(){
  return (
    <div className="App">
      <Router>
        <NavBar />
        <Switch>
         <Route exact path='/' component={HomepageLayout} />  
         <Route exact path='/:topic' component={Topic} />  
         <Route path='*' component={NotFound} />
        </Switch>
      </Router>
    </div>
  );
 }

export default App;

I've also tried not specifying a path, as some have advised, but this didn't work either:

<Route component={NotFound} />

Any ideas?

Jon
  • 622
  • 8
  • 29

1 Answers1

3

This will not work as you are mentioning in the second route as any value can hold as /:topic, this route will get displayed when you add like this. Because, any segment that starts with a colon will be treated as a parameter. So the * route will not get displayed at any case!

Veno
  • 433
  • 2
  • 14