How to set the DefaultRoute to another Route in React Router

前端 未结 11 1207
心在旅途
心在旅途 2020-12-13 02:11

I have the following:

  
    
            


        
相关标签:
11条回答
  • 2020-12-13 02:16

    The preferred method is to use the react router IndexRoutes component

    You use it like this (taken from the react router docs linked above):

    <Route path="/" component={App}>
        <IndexRedirect to="/welcome" />
        <Route path="welcome" component={Welcome} />
        <Route path="about" component={About} />
    </Route>
    
    0 讨论(0)
  • 2020-12-13 02:17

    I ran into a similar issue; I wanted a default route handler if none of the route handler matched.

    My solutions is to use a wildcard as the path value. ie Also make sure it is the last entry in your routes definition.

    <Route path="/" component={App} >
        <IndexRoute component={HomePage} />
        <Route path="about" component={AboutPage} />
        <Route path="home" component={HomePage} />
        <Route path="*" component={HomePage} />
    </Route>
    
    0 讨论(0)
  • 2020-12-13 02:23

    UPDATE : 2020

    Instead of using Redirect, Simply add multiple route in the path

    Example:

    <Route exact path={["/","/defaultPath"]} component={searchDashboard} />
    
    0 讨论(0)
  • 2020-12-13 02:25

    For those coming into 2017, this is the new solution with IndexRedirect:

    <Route path="/" component={App}>
      <IndexRedirect to="/welcome" />
      <Route path="welcome" component={Welcome} />
      <Route path="about" component={About} />
    </Route>
    
    0 讨论(0)
  • 2020-12-13 02:25

    You use it like this to redirect on a particular URL and render component after redirecting from old-router to new-router.

    <Route path="/old-router">
      <Redirect exact to="/new-router"/>
      <Route path="/new-router" component={NewRouterType}/>
    </Route>
    
    0 讨论(0)
  • 2020-12-13 02:26

    You can use Redirect instead of DefaultRoute

    <Redirect from="/" to="searchDashboard" />
    

    Update 2019-08-09 to avoid problem with refresh use this instead, thanks to Ogglas

    <Redirect exact from="/" to="searchDashboard" />
    
    0 讨论(0)
提交回复
热议问题