I have an event handler on a button in React.
The component that holds button takes in a prop: userInput, from an input field in the parent.
Then it uses this to update the state: bLength. When I console.log bLength, it is the old value. Also, I've tried setTimeout to 3000ms and it runs immediately.
handleClick = e => {
var { userInput } = this.props;
this.setState(() => ({
bLength: userInput,
}));
console.log(this.state.bLength);
setTimeout(console.log(this.state.bLength), 3000);
}
};
State of bLength
is initially set to 3
.
User inputs 6
User clicks button
Console logs 3
User clicks button AGAIN
Console logs 6
.
I thought passing a function to setState eliminates the problem of setState's async uncertainty.
Why is this happening?