我有一个这样的嵌套路由:
<Route path="modules" component={Modules}>
<Route path=":moduleId" component={Module}/>
</Route>
我注意到 Module 在 Modules 挂载之前挂载。这是嵌套路由的预期功能吗? 我问是因为我在 Modules 中收集了一些数据,需要解决这些数据才能使 Module 呈现为有意的。
如果我能看到 Modules 在安装之前就已呈现,这怎么可能?
最佳答案
这不是特定于 react-router
。在 React Component
生命周期中:
child 先于 parent 。
componentWillMount
先运行子节点。parent 先于 child 下车。
componentWillUnmount
先运行父级。
这样想:如果您正在 build 某样东西,首先您会逐渐将较小的部分连接到较大的部分,直到您拥有完整的东西。如果您要拆解它,请先拆下较大的部件,然后再拆解每个部件,直到您回到组件上。
关于javascript - react-router:嵌套路由组件在父路由组件之前安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045056/