简体   繁体   中英

In React Router Dom, redirect not work if i don't press enter or refresh page

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM