in React JS by
Q:

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

► Click here to show 2 Answers

0 votes
by

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

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

React JS
Learn More with Madanswer

Related questions

0 votes
asked Jul 2, 2019 in React JS by Venkatshastri
...