A callback function should be called when setState has finished, and the component is re-rendered.
As the setState is asynchronous, which is why it takes in a second callback function.
A callback function should be called when setState has finished, and the component is re-rendered.
As the setState is asynchronous, which is why it takes in a second callback function.
A callback function is a function passed as an argument to another function, to be called later when some task is finished or an event happens.
function loadData(done) {
setTimeout(() => done("data ready"), 1000);
}
loadData(function (msg) { console.log(msg); }); // callbackPurpose:
onClick={handleClick} in React.setState is asynchronous, so it accepts a callback as a second argument that runs after the state has updated and the component has re-rendered:this.setState({ count: 1 }, () => console.log(this.state.count));Log in or create a free account to answer this question.