-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix componentDidUpdate when updating by setState on v16 #1133
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,3 +41,4 @@ | |
| "react-dom": "^15.5.0" | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,7 +369,40 @@ class ShallowWrapper { | |
| } | ||
| this.single('setState', () => { | ||
| withSetStateAllowed(() => { | ||
| this.instance().setState(state, callback); | ||
| const adapter = getAdapter(this[OPTIONS]); | ||
| const instance = this.instance(); | ||
| const prevProps = instance.props; | ||
| const prevState = instance.state; | ||
| const prevContext = instance.context; | ||
| let shouldRender = true; | ||
| // This is a dirty hack but it requires to know the result of shouldComponentUpdate. | ||
| // When shouldComponentUpdate returns false we shouldn't call componentDidUpdate. | ||
| // shouldComponentUpdate is called in `instance.setState` | ||
| // so we replace shouldComponentUpdate to know the result and restore it later. | ||
| let originalShouldComponentUpdate; | ||
| if ( | ||
| this[OPTIONS].lifecycleExperimental && | ||
| adapter.options.enableComponentDidUpdateOnSetState && | ||
| instance && | ||
| typeof instance.shouldComponentUpdate === 'function' | ||
| ) { | ||
| originalShouldComponentUpdate = instance.shouldComponentUpdate; | ||
| instance.shouldComponentUpdate = (...args) => { | ||
| shouldRender = originalShouldComponentUpdate.apply(instance, args); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this function also I wonder if this should use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, I can write this like the following if I use let shouldRender = true;
let spy;
if (instance.shouldComponentUpdate) {
spy = sinon.spy(instance, 'shouldComponentUpdate');
}
instance.setState(state, callback);
if (spy) {
shouldRender = spy.getCall(0).returnValue;
spy.restore();
}Also
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to avoid Proxy; I'm not sure it would work here anyways, and it would very tightly constrict the engines enzyme can run on.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. To support
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A followup PR is always appreciated :-D
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll work on it 👍 |
||
| instance.shouldComponentUpdate = originalShouldComponentUpdate; | ||
| return shouldRender; | ||
| }; | ||
| } | ||
| instance.setState(state, callback); | ||
| if ( | ||
| shouldRender && | ||
| this[OPTIONS].lifecycleExperimental && | ||
| adapter.options.enableComponentDidUpdateOnSetState && | ||
| instance && | ||
| typeof instance.componentDidUpdate === 'function' | ||
| ) { | ||
| instance.componentDidUpdate(prevProps, prevState, prevContext); | ||
| } | ||
| this.update(); | ||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prevContext should be passed into componentDidUpdate; is react not going to fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. #1139 is a PR for that.
You can see the change at [Unreleased] section in CHANGELOG.md
https://github.com/facebook/react/blob/master/CHANGELOG.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then if that's fixed, why do we need an option in the adapter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is to support that componentDidUpdate no longer receives prevContext argument, which is not only ShallowRenderer.
This PR is to support that ShallowRenderer no longer call componentDidUpdate.
These are different fixes so I added the options each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does componentDidUpdate not receive a prevContext argument anymore? It seems like it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason seems to be the following. cc: @bvaughn
facebook/react#8631 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that seems like a pretty major change to the way context works then.
I've not found context to be flaky at all in React <= 15.
@lelandrichardson any suggestions for a better name for this part of the adapter interface?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: My previous comment linked to the same response @koba04 did. Totally not helpful. Sorry. 😄 It's Monday morning.
There are pretty decent notes on facebook/react/pull/8631. The discussion/debate on how to handle
prevContextin 16 stretched across 4 months. It was somewhat controversial. But I think the resolution we landed at was the best choice of those that we had. Context will need to change in the future to avoid the potential pitfalls it has always had (and still currently has).In the meanwhile, this is still good advice: