Do you want to request a feature or report a bug?
Either a bug or a feature, depending on how the team sees it 🤷♀️
What is the current behavior?
The presence of static getDerivedStateFromProps does not suppress the warning about state not being initialized, even if it is always called, even on first render.
class Foo extends React.Component {
static getDerivedStateFromProps(props) {
return { a: props.a }
}
/* // uncommenting the constructor silences the warning, but is redundant work
constructor(props) {
super(props)
this.state = { a: props.a }
}
*/
render() {
// this is slightly silly, but is an example
return this.state.a
}
}
What is the expected behavior?
getDerivedStateFromProps is always called, even on initial render, so maybe it should be considered as having initialized the state. This means that the second argument to it should be allowed to be undefined, as it would be on the first call if state is not initialized by the user. This change would not be a breaking change, as it depends on the user breaking from the pattern of initializing state in the constructor, although it brings memories of createClass's getInitialState.
Alternatively, it should not be called on initial render, and the class implementer would call its own getDerivedStateFromProps and spread its result on its initial state definition. This is clearly not possible anymore now that 16.3.0 is out.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.3.0
Do you want to request a feature or report a bug?
Either a bug or a feature, depending on how the team sees it 🤷♀️
What is the current behavior?
The presence of
static getDerivedStateFromPropsdoes not suppress the warning aboutstatenot being initialized, even if it is always called, even on first render.What is the expected behavior?
getDerivedStateFromPropsis always called, even on initial render, so maybe it should be considered as having initialized the state. This means that the second argument to it should be allowed to be undefined, as it would be on the first call if state is not initialized by the user. This change would not be a breaking change, as it depends on the user breaking from the pattern of initializing state in the constructor, although it brings memories ofcreateClass'sgetInitialState.Alternatively, it should not be called on initial render, and the class implementer would call its own getDerivedStateFromProps and spread its result on its initial state definition. This is clearly not possible anymore now that 16.3.0 is out.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
16.3.0