What you are seeing is because react-router
doesn't really use the React lifecycle process to render its children.
Instead, react-router
individually renders each <Route>
's component and then merges them together. This is why you have to include this.props.children
in a component route that has child routes. You can see the code responsible for this in the <RouterContext>
's render function.
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. 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:
- Render
<Message routeParams={{ id: 12 }} />
and store result as element
.
- Render
<Inbox children={element} />
and store result as element
.
- Render
<App children={element} />
and return result.