代码之家  ›  专栏  ›  技术社区  ›  r4id4

路由推送时反应路由器更新子组件

  •  2
  • r4id4  · 技术社区  · 7 年前

    我有一个这样的组件

    class myApp extends React.Component {
      return (
       <div>
         <NavigationBar />
         {/* dynamic_content */}
       </div> )
    }
    

    使用此路线

    <Router>
      <Route exact path="/" component={myApp}/>
    </Router>
    

    而且负载很好。
    我想实现的是,例如,当url更改时

    /profile
    /notifications
    

    这个 dynamic_content 更新,而不实际转换到另一个组件。
    在伪代码中,我想要的是

    class myApp extends React.Component {
      return (
       <div>
         <NavigationBar />
         {route === '/profile' -> <Profile/>
          route === '/notification' -> <Notification/>}
       </div> )
    

    这可能吗?
    我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Shubham Khatri    7 年前

    是的,这是可能的,您需要定义嵌套管线,如

    class MyApp extends React.Component {
      return (
       <div>
         <NavigationBar />
         {/* dynamic_content */}
         <Route path='/profile' component={Profile}/>
         <Route path="/notification" component={Notification}/>
       </div> )
    }
    

    并在没有 exact 道具

    <Router>
      <Route path="/" component={MyApp}/>
    </Router>
    

    此外,必须以大写字符作为第一个字母定义组件