在react-router v4中,我们仍然可以在App容器中执行`this.props.children`吗?

时间:2017-04-13 10:50:12

标签: react-router react-router-v4

在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}不再起作用了。它是以另一种方式完成的吗?

2 个答案:

答案 0 :(得分:0)

V4是主要更新,是&lt; = V3的完整重写。你不应期望升级到V4并让一切工作仍然有效。我建议您查看documentation examples,以便了解具体方法。这是一个基本示例,演示了您拥有的嵌套路由。

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

答案 1 :(得分: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>
    }
相关问题