You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Component with mixins: componentWillUnmount should be called in reverse order (of componentDidMount) #2789
componentDidMount() and componentWillUnmount() allow for DOM manipulation outside React's reach. DOM manipulations done in componentDidMount() should be undone in reverse order in componentWillUnmount(). This is currently not possible in components with mixins (see footnote) due to the order in which the events are fired:
changed the title [-]Component with mixins: componentWillUnmount should fire in reverse order (of componentDidMount)[/-][+]Component with mixins: componentWillUnmount should be called in reverse order (of componentDidMount)[/+]on Dec 31, 2014
Curious to hear your use case! I don't usually find myself depending on the order of mixin lifecycle hooks.
What kind of DOM manipulation do you perform in mixins?
@gaearoncomponentDidMount() can be used for constructing purposes. componentWillUnmount() can be used for destructing what was constructed in componentDidMount(). If a component builds on something a mixin has already constructed, it should be removed before the mixin additions are removed. Destructing should be in the reverse order of constructing.
Hmm, we opted for deterministic ordering of calls instead of special casing any specific lifecycle methods (see #1601). I totally get where you're coming from - we should consider this. cc @yungsters who originally proposed what we did in #1601.
@spicyj The main issue here is giving control back to the developer. Interdependent mixins can be left to the developer. Just do componentDidMount() in the order the mixins are given up (just like now), but reverse the order for componentWillUnmount() (see the example at the top).
The intent of the lifecycle methods is for componentDidMount and componentWillUnmount to mirror one another. Resources allocated or state mutated in the former should be reversed in the latter. As such, mixins that assume a particular state when mounted should be able to assume that same state when unmounting.
We're conceptually matching curly braces / HTML tags / etc. here.
See discussion on #3290 – mixins are effectively a legacy feature now and so I'm not going to merge my (breaking) change to their behavior even though I agree that it makes more sense that way. Closing as wontfix -- in most cases you can use higher-order components to get the behavior you want.
Activity
[-]Component with mixins: componentWillUnmount should fire in reverse order (of componentDidMount)[/-][+]Component with mixins: componentWillUnmount should be called in reverse order (of componentDidMount)[/+]gaearon commentedon Dec 31, 2014
Curious to hear your use case! I don't usually find myself depending on the order of mixin lifecycle hooks.
What kind of DOM manipulation do you perform in mixins?
yiminghe commentedon Jan 7, 2015
Other mixin mechanisms such as YUI widget extension is same as @rickbeerendonk proposed, it is more reasonable.
rickbeerendonk commentedon Jan 7, 2015
@gaearon
componentDidMount()can be used for constructing purposes.componentWillUnmount()can be used for destructing what was constructed incomponentDidMount(). If a component builds on something a mixin has already constructed, it should be removed before the mixin additions are removed. Destructing should be in the reverse order of constructing.syranide commentedon Jan 7, 2015
It's mentioned briefly in #1853 if I understand you correctly (just mentioning for posterity).
zpao commentedon Jan 8, 2015
Hmm, we opted for deterministic ordering of calls instead of special casing any specific lifecycle methods (see #1601). I totally get where you're coming from - we should consider this. cc @yungsters who originally proposed what we did in #1601.
gaearon commentedon Jan 8, 2015
To clarify, the point of #1601 was protecting against
behaving differently from
sophiebits commentedon Feb 27, 2015
I think I'm in favor of this, though it's not obvious what order all the update methods should be called in when the mixins are interdependent.
rickbeerendonk commentedon Feb 27, 2015
@spicyj The main issue here is giving control back to the developer. Interdependent mixins can be left to the developer. Just do
componentDidMount()in the order the mixins are given up (just like now), but reverse the order forcomponentWillUnmount()(see the example at the top).yungsters commentedon Feb 27, 2015
Sorry, did not see this until now. I am in favor of @rickbeerendonk's proposal. We should effectively reverse the order of
componentWillUnmount(), so:The intent of the lifecycle methods is for
componentDidMountandcomponentWillUnmountto mirror one another. Resources allocated or state mutated in the former should be reversed in the latter. As such, mixins that assume a particular state when mounted should be able to assume that same state when unmounting.We're conceptually matching curly braces / HTML tags / etc. here.
Call componentWill* methods in reverse order
sophiebits commentedon Apr 28, 2015
See discussion on #3290 – mixins are effectively a legacy feature now and so I'm not going to merge my (breaking) change to their behavior even though I agree that it makes more sense that way. Closing as wontfix -- in most cases you can use higher-order components to get the behavior you want.
Seldaek commentedon Oct 22, 2015
@spicyj AFAIK the "mixins are legacy and deprecated" line was modified, so shouldn't this be addressed after all?
sophiebits commentedon Oct 22, 2015
We're not planning to change their semantics, instead preferring to recommend an official ES7 mixin solution if any appears.
sophiebits commentedon Oct 22, 2015
And no, we still discourage mixins for most use cases.