0 votes
in React JS by (1.4k points)

What is the purpose of callback function as an argument of setState()?

2 Answers

0 votes
by (1.4k points)

The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.

Note: It is recommended to use lifecycle method rather than this callback function.

setState({ SurName: 'Venkat' }, () => console.log('The Sur name has been updated and component re-rendered'))
0 votes
by (23.1k points)

setState({ name: 'John' }, () => console.log('The name has updated and component re-rendered'))