I know well why we need functional setState and how it works, e.g.
this.setState((prevState, props) => ...);
You get the previous state as a parameter as above.
However pay attention to props
too in the arguments.
Here I encountered explanation about the props in functional setState:
In addition, it also applies when the update depends on props. These can become stale as well when the received props from the parent component have changed before the asynchronous execution kicks. Therefore, the function in this.setState() gets as second argument the props.
However that explanation still doesn't click to me.
Can someone bring an example how the "props" could become stale? e.g. maybe a code snippet which demonstrates a bug when using this.props instead of "props" as specified in the arguments of the callback function which setState takes?
In other words I don't get why there is need for props
argument in functional setState and would be nice to see an example why it is needed.
Yes, but you might not want to. With class components we would keep all of our state in one object. Then when we changed state with setState , we would provide an object and the API would "merge" its contents into the existing state for us. This doesn't happen with setting state in function components with useState .
Before updating the value of the state, we need to build an initial state setup. Once we are done with it, we use the setState() method to change the state object. It ensures that the component has been updated and calls for re-rendering of the component.
Functional components can accept and use props. Functional components should be favored if you do not need to make use of React state.
Props are used to pass data from one component to another. The state is a local data storage that is local to the component only and cannot be passed to other components.
class Children extends React.Component {
state = {
initial: true,
};
onClick = () => {
this.props.handler();
console.log(this.state.initial, this.props.initial); // true true
this.setState((state, props) => {
console.log(state.initial, props.initial); // true false
console.log(this.state.initial, this.props.initial); // true true
});
};
render() {
console.log("render children", this.state.initial, this.props.initial);
return <div onClick={this.onClick}>Click me</div>
}
}
class Hello extends React.Component {
state = {
initial: true,
};
handler = () => {
this.setState({initial: false});
}
render() {
console.log("render parent", this.state.initial);
return <Children initial={this.state.initial} handler={this.handler} />
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);
In this example, the children will call the handler which will change the parent state and update his own state. In the callback, you can see that props and this.props are different: props are the new values while this.props are stale.
Demo here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With