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

Don't recreate instance when resuming a class component's initial mount #9608

Merged
merged 2 commits into from
May 5, 2017

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented May 5, 2017

Recreating the class instance causes refs (and other callbacks) to close over stale instances.

Instead, re-use the previous instance. componentWillMount is called again. We also call componentWillReceiveProps, to ensure that state derived from props remains in sync.

@acdlite acdlite requested a review from sebmarkbage May 5, 2017 00:18
@acdlite acdlite force-pushed the reuseinstanceonresume branch 2 times, most recently from 87ceda5 to 2b1a0e3 Compare May 5, 2017 17:26
newInstance.props = newProps;
newInstance.state = newState = newInstance.state || null;
newInstance.context = newContext;
// componentWillMount may have called setState. Process the update queue.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is in the wrong place. Do you mean componentWillReceiveProps? If so, shouldn't this be gated on the existence of a componentWillReceiveProps?

@@ -444,6 +496,9 @@ module.exports = function(
if (typeof instance.componentDidMount === 'function') {
workInProgress.effectTag |= Update;
}

instance.state = newState;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do componentWillMount() { this.state = {}; } What should it do?

@acdlite acdlite force-pushed the reuseinstanceonresume branch from 664ea79 to 7526336 Compare May 5, 2017 21:01
acdlite added 2 commits May 5, 2017 14:03
Recreating the class instance causes refs (and other callbacks) to close
over stale instances.

Instead, re-use the previous instance. componentWillMount is called
again. We also call componentWillReceiveProps, to ensure that
state derived from props remains in sync.
@acdlite acdlite force-pushed the reuseinstanceonresume branch from 7526336 to 628b82c Compare May 5, 2017 21:03
@acdlite acdlite merged commit 628b82c into facebook:master May 5, 2017
@acdlite acdlite mentioned this pull request May 6, 2017
17 tasks
flarnie added a commit to flarnie/react that referenced this pull request May 11, 2017
**what is the change?:**
A test was added for a change to Fiber's behavior in facebook#9608, and because of a
bug in our CirclCI script it landed when failing for non-fiber runs of the
tests.

This just wraps the test in a feature flag because it seems clear it was
only intended to test the new fiber behavior.

Thanks to @gaearon for pairing on this! :)

**why make this change?:**
So that tests are passing on master.

**test plan:**
`npm run test ReactCompositeComponentState`

**issue:**
None - figured it out before anyone opened an issue afaik.
flarnie added a commit to flarnie/react that referenced this pull request May 11, 2017
**what is the change?:**
A test was added for a change to Fiber's behavior in facebook#9608, and because of a
bug in our CirclCI script it landed when failing for non-fiber runs of the
tests.

This just wraps the test in a feature flag because it seems clear it was
only intended to test the new fiber behavior.

Thanks to @gaearon for pairing on this! :)

**why make this change?:**
So that tests are passing on master.

**test plan:**
`npm run test ReactCompositeComponentState`

**issue:**
None - figured it out before anyone opened an issue afaik.
flarnie added a commit that referenced this pull request May 11, 2017
**what is the change?:**
A test was added for a change to Fiber's behavior in #9608, and because of a
bug in our CirclCI script it landed when failing for non-fiber runs of the
tests.

This just wraps the test in a feature flag because it seems clear it was
only intended to test the new fiber behavior.

Thanks to @gaearon for pairing on this! :)

**why make this change?:**
So that tests are passing on master.

**test plan:**
`npm run test ReactCompositeComponentState`

**issue:**
None - figured it out before anyone opened an issue afaik.
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.

3 participants