Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

componentDidUpdate prevState is not previous state #2914

Closed
kevzettler opened this issue Jan 22, 2015 · 1 comment
Closed

componentDidUpdate prevState is not previous state #2914

kevzettler opened this issue Jan 22, 2015 · 1 comment

Comments

@kevzettler
Copy link

http://facebook.github.io/react/docs/component-specs.html#updating-componentdidupdate

componentDidUpdate: function(prevProps, prevState){
            debugger;
                //...
}
this.state.tags.length
2
prevState.tags.length
2
@zpao
Copy link
Member

zpao commented Jan 22, 2015

Presumably this.state.tags is an array and you have pushed into it, then called setState with it again.

Something like this:

update: function() {
  var tags = this.state.tags;
  tags.push('new tag');
  this.setState({tags: tags});
}

That will keep the reference to the same array instance, which is why prevState and state are the same.

However if you actually cloned the array you wouldn't run into this reference issue (unless you looked at further nested objects)

update: function() {
  var tags = this.state.tags.slice();
  tags.push('new tag');
  this.setState({tags: tags});
}

I don't think that we want to make this work properly. It would require deep cloning current state on every transition which can get expensive. Other libraries are better at this, things like Immutable.js and Mori are good examples.

cc @sebmarkbage do you're aware. Reopen if you think we should do anything about this ourselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants