简体   繁体   中英

ReactJS Route 404 '*' redirecting all the time

Im having a little problem here, Im trying to put a page 404 when the url is invalid, the problem is, it's loading all the time, for example if a try to access my home page will redirect to the page 404, I've tried some examples but not success :

#redirect in all pages
<Redirect exact={true} from='*' to='/404' /> #test 1
<Redirect from='*' to='/404' /> #test2

#appear in the bottom of the pages
<Route exact={true} path='*' component={asyncComponent(() => import('./containers/Page/404'))} />
<Route path='*' component={asyncComponent(() => import('./containers/Page/404'))} />

try having in this way

<Switch>
  <Route path="/" exact component={Home}/>
  <Route path="/will-match" component={WillMatch}/>
  <Route component={NoMatch} />
</Switch>

React router docs for no match

This is how I have configured the routes in a recent react project.

<Route exact path={`${process.env.PUBLIC_URL}/`} component={Main} />
<Route path='/new' component={ReportCreator}/>
<Route path='/reports/:id' component={Report}/>
<Route component={Page404} />

Using BrowserRouter and Switch router.

https://reacttraining.com/react-router/web/example/no-match

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