在react-router v4中使用React IndexRoute

Quo*_*ran 45 javascript node.js reactjs react-router react-router-v4

我正在学习React自己的在线教程.

所以这是使用React Router的基本示例:

<Router history={browserHistory}>
  <Route path="/" component={App}>
    <IndexRoute component={Home}/>
    <Route path="/home" component={Home}/>
    <Route path="/about" component={About}/>
    <Route path="/contact" component={Contact}/>
  </Route>
</Router>
Run Code Online (Sandbox Code Playgroud)

使用我的App组件:

class App extends React.Component {
   render() {
      return (
         <div>
            <ul>
              <li><Link to="home">Home</Link></li>
              <li><Link to="about">About</Link></li>
              <li><Link to="contact">Contact</Link></li>
           </ul>
          {this.props.children}
        </div>
     )
   }
}
export default App;
Run Code Online (Sandbox Code Playgroud)

但是,我在使用IndexRoute时遇到问题,因为它没有显示任何内容,所以我在npm上搜索react-router-dom v4的模块,里面没有IndexRoute.相反,它使用这个:

<Router>
  <div>
  <ul>
    <li><Link to="/">Home</Link></li>
    <li><Link to="/about">About</Link></li>
    <li><Link to="/contact">Contact</Link></li>
  </ul>
  <hr/>
  <Route exact path="/" component={Home}/>
  <Route path="/about" component={About}/>
  <Route path="/contact" component={Contact}/>
  </div>
</Router>
Run Code Online (Sandbox Code Playgroud)

那么如何为1条路径渲染2个组件呢?

Dei*_*kas 66

更新 react-router-4已经改变,因为它不再有孩子.但是,使用该Route组件,您可以渲染与路径匹配的任何内容.

<Router>
  <div>
    <ul>
      <li><Link to="/">Home</Link></li>
      <li><Link to="/about">About</Link></li>
      <li><Link to="/contact">Contact</Link></li>
    </ul>
    <hr/>

    // All 3 components below would be rendered when in a homepage
    <Route exact path="/" component={Home}/>
    <Route exact path="/" component={About}/>
    <Route exact path="/" component={Contact}/>

    <Route path="/about" component={About}/>
    <Route path="/contact" component={Contact}/>
  </div>
</Router>
Run Code Online (Sandbox Code Playgroud)

请参阅此bin以试验它https://www.webpackbin.com/bins/-Kf5cpLcax4dttAEvkCW.

这意味着如果你想要一个包装器,你可以在组件内部编写它.


xgq*_*rms 14

反应路由器&没有IndexRoute任何更多

<Route exact path="/" component={Home}/>

===

<IndexRoute component={Home}/>

https://github.com/ReactTraining/react-router/issues/4732#issuecomment-317239220

# Switch


```jsx

<Route path="/about" component={About}/>
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>

import { Switch, Route } from 'react-router'

<Switch>
    <Route exact path="/" component={Home}/>
    <Route path="/about" component={About}/>
    <Route path="/:user" component={User}/>
    <Route component={NoMatch}/>
</Switch>


/* there will only ever be one child here */

<Fade>
    <Switch>
        <Route/>
        <Route/>
    </Switch>
</Fade>

<Fade>
    <Route/>
    <Route/>
</Fade>

/* there will always be two children here, one might render null though, making transitions a bit more cumbersome to work out */

```

https://reacttraining.com/react-router/web/api/Switch
Run Code Online (Sandbox Code Playgroud)