React: [bug?] componentWillUnmount calls not called before owning parent's.

Created on 1 Sep 2015  路  2Comments  路  Source: facebook/react

For details, see here: https://github.com/pilwon/react-famous/issues/21

Basically, it seems that when a component will be unmounted, it's componentWillUnmount method needs to be called _after_ those of the elements that it owns.

This issue is related to https://github.com/facebook/react/issues/2789 and should probably be addressed (although the part about mixins can be ignored).

Most helpful comment

This is intentional. componentDidMount of a parent gets called after componentDidMount of its children, so you know your children are fully initialized before using them. Likewise, componentWillUnmount is called _before_ children (the reverse of componentDidMount) so that you have a chance to reverse anything that you might have done in componentDidMount.

For react-famous, probably an alternate rendering backend is more appropriate, more like react-art or react-canvas or react-native but we don't yet have a good supported way to do that. (Unlike the lifecycle method, that would allow you to do the _actual_ unmounting in the order you prefer.) Your solution of removing the children from the parent sounds pretty reasonable though.

All 2 comments

By console.logging inside of componentWillUnmount methods of all my components, I can indeed see that the calls happen in pre-order, but IMHO they should happen in post-order.

I haven't verified, but as reasoned in #2789, it makes sense to do the mounting in reverse of the unmounting (pre-order).

This is intentional. componentDidMount of a parent gets called after componentDidMount of its children, so you know your children are fully initialized before using them. Likewise, componentWillUnmount is called _before_ children (the reverse of componentDidMount) so that you have a chance to reverse anything that you might have done in componentDidMount.

For react-famous, probably an alternate rendering backend is more appropriate, more like react-art or react-canvas or react-native but we don't yet have a good supported way to do that. (Unlike the lifecycle method, that would allow you to do the _actual_ unmounting in the order you prefer.) Your solution of removing the children from the parent sounds pretty reasonable though.

Was this page helpful?
0 / 5 - 0 ratings