简体   繁体   English

react-router:嵌套路由组件在父路由组件之前安装?

[英]react-router: Nested route component mounts before parent route component?

I have a nested route like this: 我有一个这样的嵌套路线:

<Route path="modules" component={Modules}>
  <Route path=":moduleId" component={Module}/>
</Route>

I noticed that Module is mounting before Modules mounts. 我注意到Module模块安装之前正在安装。 Is this the intended functionality of nested routes? 这是嵌套路由的预期功能吗? I ask because I have some data gathering happening in Modules that needs to be resolved in order for Module to render as intended. 我问,因为我在模块中发生了一些需要解决的数据收集,以便模块按预期进行渲染。

How is this possible if I can see that Modules has rendered before it even mounts? 如果我能看到模块在安装之前已经渲染,那么这怎么可能呢?

This is not specific to react-router . 这不是react-router特有的。 In the React Component life-cycle: 在React Component生命周期中:

  • Children are mounted before their parents. 孩子们在父母面前。 componentWillMount runs child-first. componentWillMount以child-first运行。

  • Parents are unmounted before their children. 父母在孩子面前没有下台。 componentWillUnmount runs parent-first. componentWillUnmount运行parent-first。

Think about it this way: if you're constructing something, first you attach the smaller parts to the larger parts, progressively, until you have the complete thing. 以这种方式思考:如果你正在构建某些东西,首先要将较小的部分逐渐附加到较大的部分,直到你拥有完整的东西。 If you're dismantling it, first you remove the larger pieces, and then disassemble each of those pieces, until you're back to the components. 如果您正在拆卸它,首先要移除较大的部件,然后拆卸每个部件,直到您回到部件上。

What you are seeing is because react-router doesn't really use the React lifecycle process to render its children. 你看到的是因为react-router并没有真正使用React生命周期过程来渲染它的子进程。

Instead, react-router individually renders each <Route> 's component and then merges them together. 而是, react-router单独呈现每个<Route>的组件,然后将它们合并在一起。 This is why you have to include this.props.children in a component route that has child routes. 这就是为什么你必须在具有子路由的组件路由中包含this.props.children You can see the code responsible for this in the <RouterContext> 's render function. 您可以在<RouterContext>的render函数中看到对此负责的代码。

Essentially what it does is that it takes the most nested route component and renders it. 它本质上所做的是它采用最嵌套的路由组件并呈现它。 Then, it takes the next most nested component and renders that with the previously rendered component as its children prop. 然后,它接受下一个最嵌套的组件,并使用先前渲染的组件作为其children prop。 This continues down the line until you reach the root route component. 这将继续沿着直线行进,直到到达根路径组件。

Given the routes: 鉴于路线:

<Route path="/" component={App}>
  <Route path="inbox" component={Inbox}>
    <Route path="messages/:id" component={Message} />
  </Route>
</Route>

When you visit /inbox/messages/12 , react-router will do the following: 当您访问/inbox/messages/12react-router将执行以下操作:

  1. Render <Message routeParams={{ id: 12 }} /> and store result as element . 渲染<Message routeParams={{ id: 12 }} />并将结果存储为element
  2. Render <Inbox children={element} /> and store result as element . 渲染<Inbox children={element} />并将结果存储为element
  3. Render <App children={element} /> and return result. 渲染<App children={element} />并返回结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM