1

I have a strange problem with React Router Dom. In my code I have:

if (this.state.area) {
return (<Redirect push to = {'/ service_list'} />)
}

In the url it correctly writes "localhost: 3000 / list_services, but it seems not to load the page. If I press enter, or reload the page, it is displayed correctly. Can anyone help me? In my route file i have:

<Route exact path="/emilia_romagna">
  <Home/>
</Route>
<Route exact path="/lista_servizi">
  <ListaServizi/>
</Route>
Parma Libera
  • 166
  • 1
  • 16
  • Your route is matching `lista_servizi`, not `service_list` or `list_services`? – BenM Mar 13 '21 at 11:38
  • Are you using `BrowserRouter` or `Router`? – Ajeet Shah Mar 13 '21 at 13:17
  • BrowserRouter as Router – Parma Libera Mar 13 '21 at 16:59
  • Can you edit your code to show "how" you imported and used it? Also, how are you doing redirect? Are you doing it in `render` function of your class component? Can you try if it is working with `history.push("/some/path")`? If nothing works, consider creating a codepen or codesandbox and provide us the link. – Ajeet Shah Mar 13 '21 at 17:02

1 Answers1

0

Rewrite to this model:

import { BrowserRouter, Route } from "react-router-dom";

<BrowserRouter>
    <Route exact path="/emilia_romagna" component={Home} />
    <Route exact path="/lista_servizi" component={ListaServizi} />
    <Router component={NotFound} /> // Add this line and write the Not Found component to ensure that other paths mapped correctly.
</BrowserRouter>
Ali Torki
  • 1,929
  • 16
  • 26