Skip to content

Call componentWill* methods in reverse order - #3290

Closed
sophiebits wants to merge 1 commit into
react:masterfrom
sophiebits:gh-2789
Closed

sophiebits wants to merge 1 commit into
react:masterfrom
sophiebits:gh-2789

Conversation

@sophiebits

Copy link
Copy Markdown
Collaborator

This ordering mirrors what you would see if each mixin was implemented as a wrapper component instead of a mixin, and allows any setup and teardown routines to see the component in a consistent state.

Fixes #2789.

This ordering mirrors what you would see if each mixin was implemented as a wrapper component instead of a mixin, and allows any setup and teardown routines to see the component in a consistent state.

Fixes react#2789.
@sophiebits

Copy link
Copy Markdown
Collaborator Author

I should note that in the update cycle, the new behavior here still differs somewhat from what you'd see with wrapper components – in particular, the componentWillReceiveProps, shouldComponentUpdate, and componentWillUpdate calls are interleaved this way – but they're still called in the same order relative to the other mixins and this seems the best we can do.

@sebmarkbage

Copy link
Copy Markdown
Contributor

Let's do this in 0.14?

@sophiebits

Copy link
Copy Markdown
Collaborator Author

👍, wasn't hoping for 0.13 here.

@syranide

Copy link
Copy Markdown
Contributor

This seems wrong to me. I'm guessing it only works when "applying all mixins at once", you could not do this in multiple steps in a generic way. It also seems like mixin-abuse to have interdependent but separate mixins.

I'm certainly no authority on this subject, but I suspect this is solving a case that you simply shouldn't be using them for. Consider if lifecycle methods where events instead... I would expect listeners to be executed in the order they were added, regardless of which lifecycle event it is.

@sophiebits

Copy link
Copy Markdown
Collaborator Author

@syranide The mixins don't really need to be interdependent. But you could imagine having something like:

var A = {
  componentDidMount: function() {
    var node = React.findDOMNode(this);
    node.appendChild(...);
  },
  componentWillUnmount: function() {
    var node = React.findDOMNode(this);
    node.removeChild(node.lastChild);
  }
};

var B = {
  componentDidMount: function() {
    var node = React.findDOMNode(this);
    node.appendChild(...);
  },
  componentWillUnmount: function() {
    var node = React.findDOMNode(this);
    node.removeChild(node.lastChild);
  }
};

and you would expect the removeChild calls to remove the element that that mixin added, which is not what happens now. (You can imagine the same sort of problem cropping up in a situation without DOM manipulation too.)

@syranide

Copy link
Copy Markdown
Contributor

@spicyj

  componentDidUpdate: function() {
    var node = React.findDOMNode(this);
    node.lastChild.textContent = 'foobar';
  }

... so your example is incomplete, regardless it's easily fixed, but that's somewhat besides my point perhaps. I would still consider those "interdependent of sorts", they both interact with a single resource and require it to be in a specific state, call it "conflicting mixins" if you like.

Anyway... that's not really my argument. I'd say my main argument is lifecycle methods as events... if the answer to whether certain lifecycle events should fire listeners in reverse order then this seems fine, but that seems bad bad bad to me. You don't do that.

So it seems to me that this is complicating the logic and solving something that "shouldn't be solved", it's solving issues arising from misuse of the mixin concept.

@syranide

Copy link
Copy Markdown
Contributor

PS. Hmm, isn't it just componentWillUnmount that should be called in reverse order, why are the other lifecycle methods reversed too?

@sebmarkbage

Copy link
Copy Markdown
Contributor

I agree with @syranide here but I'll bail out on this one. The diff looks good to me, but this is super fragile and someone might also be depending on the reverse order.

If this was a fresh new feature, this change would make sense, but it seems like we're unnecessarily breaking backward compatibility. This is exactly one of the reasons I want to move away from React mixins. Effectively this is breaking backwards compatibility for a feature we're already considering legacy.

@sophiebits

Copy link
Copy Markdown
Collaborator Author

I'll leave this unmerged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Component with mixins: componentWillUnmount should be called in reverse order (of componentDidMount)

4 participants