2

在 react-router v2 中,我们可以做

// inside of routes.js

export default (
  <Route path="/" component={App}>
    <IndexRoute component={PostsIndex} />
    <Route path="/posts/new" component={PostsNew} />
    <Route path="/posts/:id" component={PostsShow} />
  </Route>
);

然后在 App 容器内显示正确的子项:

// inside app.js

{this.props.children}

但在 react-router v4 中,{this.props.children}不再起作用。是用另一种方式完成的吗?

4

2 回答 2

0

我遇到了同样的问题,我花了几个小时才找到解决方案

 const PageNavWithChildrenComponentInsideLayout = ({ match }) => {

      return <div>
          <ul>
            <li>
              <NavLink className="nav-link" to={"/consultant/childOne"} activeClassName={`${match.url}/childOne` ? "active": ""}>
              </NavLink>
               <NavLink className="nav-link" to={"/consultant/childTwo"} activeClassName={`${match.url}/childTwo` ? "active": ""}>
              </NavLink>
               <NavLink className="nav-link" to={"/consultant/childThree"} activeClassName={`${match.url}/childThree` ? "active": ""}>
              </NavLink>
            </li>
          </ul>

          {/**
            Layout is Wrapper component corresponds parent v2,v3
          **/}

         <Layout>

          {/**corresponds {this.props.children}**/}

           <Route exact path={`${match.url}/childOne`} component={ChildOne}/>
           <Route exact path={`${match.url}/childTwo`} component={ChildTwo}/>
           <Route exact path={`${match.url}/childThree`} component={ChildThree}/>

          </Layout>

        </div>;
    };

    export default PageNavWithChildrenComponentInsideLayout 

    {/**-------------Routes.js--------------*/}


    export default routes=()=>{

    <Switch>
     <Route path="/PageNavWithChildrenComponentInsideLayout" name="Parent" component={PageNavWithChildrenComponentInside}>
      <Route path="/PageNavWithChildrenComponentInsideLayout/childOne" name="ChildOne" component={ChildOne} />
      <Route path="/PageNavWithChildrenComponentInsideLayout/childTwo" name="ChildTwo" component={ChildTwo} />
      <Route path="/PageNavWithChildrenComponentInsideLayout/childThree" name="ChildThree" component={ChildThree} />
     </Route>
    </Switch>
    }
于 2017-07-25T21:17:03.080 回答
0

V4 是一个重大更新,是对 <=V3 的完全重写。您不应该期望升级到 V4 并且一切正常。我建议您查看文档示例,以便了解事情是如何工作的。这是一个基本示例,它演示了像您一样的嵌套路由。

import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'

const BasicExample = () => (
  <Router>
    <div>
      <ul>
        <li><Link to="/">Home</Link></li>
        <li><Link to="/about">About</Link></li>
        <li><Link to="/topics">Topics</Link></li>
      </ul>

      <hr/>

      <Route exact path="/" component={Home}/>
      <Route path="/about" component={About}/>
      <Route path="/topics" component={Topics}/>
    </div>
  </Router>
)

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
)

const About = () => (
  <div>
    <h2>About</h2>
  </div>
)

const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>
          Rendering with React
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>
          Components
        </Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>
          Props v. State
        </Link>
      </li>
    </ul>

    <Route path={`${match.url}/:topicId`} component={Topic}/>
    <Route exact path={match.url} render={() => (
      <h3>Please select a topic.</h3>
    )}/>
  </div>
)

const Topic = ({ match }) => (
  <div>
    <h3>{match.params.topicId}</h3>
  </div>
)

export default BasicExample
于 2017-04-13T18:05:10.057 回答