React-router – No Match (404 page) for subpages

Viewed 303

I have a small problem with page 404, for main tabs everything works fine, but for subpages NEWS no. Can anyone help with a solution?

function App() { return ( <>

    <NavLink to="/about">About</NavLink>
    <NavLink to="/news">News</NavLink>

    <section>
      <Switch>
        <Route path="/about" render={() => (<h1>About</h1>)} />
        <Route exact path="/news" render={() => (<>
          <h1>News</h1>
          <Link to="/news/n1">First</Link>
          <Link to="/news/n2">Second</Link>
        </>)} />
        <Route path="/news/:page" render={() => (<h1>Subpage</h1>)} />
        <Route render={() => (<h1>404 No match</h1>)} />
      </Switch>
    </section>

  </BrowserRouter>

</>

); } `

1 Answers

For the /news route you have the exact prop. This prop will prevent the router from accessing those <Link> components unless the route is exactly /news

Remove exact if you want the Links inside of the Route to render

Related