-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
Write blog post about new async UNSAFE_ and static lifecycles #12047
Comments
Not sure where to stick this, but we've been playing with gDSFP in react-bootstrap and one thing that tripped us up was that you shouldn't just spread through Worth mentioning in the docs (or if it's not intended i can provided a repro) |
Could you provide a repro? I don't think I understand why spreading |
(it's not so much that they are dropped as overridden) |
Cool, thanks for the repro 👍 |
I think you're saying you'd expect it to work like state = { local: 'Not updated', outer: this.props.outer }
componentWillReceiveProps(nextProps) {
this.setState(prevState => ({
...prevState,
outer: nextProps.outer
}));
} but it works more like state = { local: 'Not updated', outer: this.props.outer }
componentWillReceiveProps(nextProps) {
this.setState({
...this.state,
outer: nextProps.outer
});
} ? |
Jason's sandbox looks like a bug to me. I haven't had a chance to dig in yet (in the middle of something) but it's in my queue to look into soonish. |
@gaearon yeah, provided its not a bug, it's less intuitive (to me anyway) that |
@jquense Fair enough—aside from the bug discussion, the part about this not being obvious is unfortunate, but that's already the case for |
This isn't the case- and I'd welcome suggestions to the in-progress docs to make this more clear. (I realize I haven't yet pushed the |
Looks like this behavior is specific to let childInstance;
class Child extends React.Component {
state = { local: 0 };
static getDerivedStateFromProps(nextProps, prevState) {
return { ...prevState, outer: nextProps.outer };
}
updateState = () => {
this.setState(state => ({ local: state.local + 1 }));
this.props.onChange(this.state.outer + 1);
};
render() {
childInstance = this;
return (
<div onClick={this.updateState}>{`outer:${this.state.outer}, local:${
this.state.local
}`}</div>
);
}
}
class Parent extends React.Component {
state = { outer: 0 };
handleChange = outer => {
this.setState({ outer });
};
render() {
return <Child outer={this.state.outer} onChange={this.handleChange} />;
}
}
const instance = ReactTestUtils.renderIntoDocument(<Parent />);
const element = ReactDOM.findDOMNode(instance);
expect(element.textContent).toBe("outer:0, local:0");
// Regular setState
childInstance.updateState();
expect(element.textContent).toBe("outer:1, local:1");
// Batched setState
ReactTestUtils.Simulate.click(element);
expect(element.textContent).toBe("outer:2, local:2"); Only the second update (from the simulated click) demonstrates the unexpected behavior. |
The second example doesn't batch updates. Therefore, two |
I'm going to go ahead and close this issue since reactjs/react.dev/pull/596/ and reactjs/react.dev/pull/587 pretty well cover it (even though we can't merge them yet). |
Follow up item for PR #12028
This blog post should correspond with the 16.3 release. Its primary purpose is to let people know how to prepare for subsequent 16.4 release.
For application developers, this post should explain how to run the codemod to rename the deprecated methods (reactjs/react-codemod/pull/195) as well as general strategies for how to use the new lifecycle(s) instead.
For library maintainers, this should provide recommended update and release strategies including:
render
(to remain backwards compatible) vs what needs to either remain inUNSAFE_componentWillReceiveProps
or moved to the newstatic getDerivedStateFromProps
method.UNSAFE_
methods.The text was updated successfully, but these errors were encountered: