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

Shallow renderer: pass component instance to setState updater as this #12784

Merged
merged 2 commits into from
May 11, 2018

Conversation

Hypnosphi
Copy link
Contributor

This makes react-test-renderer behaviour aligned with one of react-reconciler, see

partialState = payload.call(instance, prevState, nextProps);

@@ -298,7 +298,7 @@ class Updater {
const currentState = this._renderer._newState || publicInstance.state;

if (typeof partialState === 'function') {
partialState = partialState(currentState, publicInstance.props);
partialState = partialState.call(publicInstance, currentState, publicInstance.props);
Copy link
Contributor

Choose a reason for hiding this comment

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

Run yarn prettier to reformat this file and CI should pass! 😄

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2018

It's kinda weird that we support that btw. I wonder why.

@Hypnosphi
Copy link
Contributor Author

Hypnosphi commented May 11, 2018

One possible usecase is to have updaters as instance methods:

this.setState(this.increment)

Of course, increment wouldn't use this and could be just a top-level function, but maybe some more complex updaters couldn't

@gaearon
Copy link
Collaborator

gaearon commented May 11, 2018

but maybe some more complex updaters couldn't

Yeah, the problem is that if you need something instance-y in the updater usually it means the code is wrong. Anyway, thanks for fixing.

@gaearon gaearon merged commit 4f459bb into facebook:master May 11, 2018
@Hypnosphi Hypnosphi deleted the setstate-bind-this branch May 11, 2018 17:38
NMinhNguyen referenced this pull request in enzymejs/react-shallow-renderer Jan 29, 2020
…s` (#12784)

* Shallow renderer: pass component instance to setState updater as `this`

* Run prettier
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.

4 participants