2017-04-13 41 views
2

在反應路由器V2,我們可以做在react-router v4中,我們還可以在App容器中執行`this.props.children`嗎?

// 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> 
); 

,然後顯示正確的子應用程序容器內:

// inside app.js 

{this.props.children} 

但在反應路由器V4,{this.props.children}沒有任何工作更多。它以另一種方式完成了嗎?

回答

0

V4是一個重大更新,是一個完整的重寫< = 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 
+2

所以你說你不行。這意味着最受歡迎的功能之一現在被打破了。加上這個答案,回答了一個不同的問題,當問題是關於this.props.children(即嵌套道具)時,這個答案是關於巢穴路線的。這個答案本來可以縮短很多「呃也許 - 去閱讀文檔」就是這個答案所增加的一切。 –

+0

@BoxandCox我讀了你的評論3次,我仍然不確定你實際上想說什麼。 –

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> 
    } 
+0

請不要張貼代碼的截圖。粘貼代碼,選擇它,按下Ctrl-K將其格式化。 – FTP

相關問題