concept callback in category react

appears as: callbacks, callback, callback
React in Action

This is an excerpt from Manning's book React in Action.

setState takes a function to use to set the new state for the component and an optional callback function. The updater function has this signature:

(prevState, props) => stateChange

With past versions of React you could pass an object instead of a function as the first argument to setState. A key difference from current versions of React (16 and up) is that it could imply that setState was synchronous in nature, whereas what happens is that React will schedule a change to state. The callback format better communicates this idea and is generally more consistent with React’s overall declarative asynchronous paradigms: you allow the system (React) to schedule updates where order but not time are guaranteed. This is in line with a more declarative approach to UI and is generally much easier to think about than having to imperatively specify updates to data at different times (often a source of race conditions).