-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
State updated before call to shouldComponentUpdate in shallow render #1970
Comments
Try |
@ljharb just tried it and it doesn't help |
@hisuwh I see that If so, I can reproduce that it works as expected behavior in browser(reproduce here). I also checked it will work in class SimpleComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
value: this.props.value
}
}
static getDerivedStateFromProps(props, state) {
if (props.value === state.value) {
return null;
}
console.log("updating state: ", props.value);
return {
value: props.value
};
}
shouldComponentUpdate(nextProps, nextState) {
console.log("state: ", this.state);
console.log("nextState: ", nextState);
console.log("props: ", this.props);
console.log("nextProps: ", nextProps);
const shouldUpdate = nextState.value !== this.state.value;
console.log("shouldUpdate: ", shouldUpdate);
return shouldUpdate;
}
render() {
console.log("render: ", this.state.value);
return (
<input value={this.props.value} />
);
}
}
// the test will pass with `mount`
// won't pass with `shallow`
const wrapper = mount(<SimpleComponent value="initial" />);
expect(wrapper.find("input").prop("value")).to.equal("initial");
wrapper.setProps({ value: "updated" });
expect(wrapper.find("input").prop("value")).to.equal("updated");
}) |
@chenesan yh it should have been state - I've corrected it. I have changed my test code to use mount as a workaround - though this is not ideal as I now need to handle more of the inner logic of the component. Shallow is implementing most of these lifecycle methods correctly but not this one. |
ok, I'll look into this. |
After some investigation I found out that |
@hisuwh the fix is merged into react and I think this issue should be fixed after react publishing new patch version. |
@chenesan once the react-test-render change is released, would you open a PR with the relevant test cases, and a version bump? <3 please feel free to open the PR with tests ASAP :-D |
I'll send PR after fix in react-test-renderer is released ;) |
Current behavior
Given a simple component like this:
I would expect the following test to pass:
However it fails as
shouldComponentUpdate
returnsfalse
. Looking at the logs we see the following:As you can see from the above
this.state
appears to have already been updated by the timeshouldComponentUpdate
gets called resulting in it returningfalse
and the test failing as it never re-renders.If I run the same component in the browser I get the following output:
Which is what I would expect.
Expected behavior
The state of the component should not be updated before calling shouldComponentUpdate.
Your environment
API
Version
Adapter
The text was updated successfully, but these errors were encountered: