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

Technology CommunityCategory: ReactWhat is the purpose of callback function as an argument of setState?
VietMX Staff asked 4 years ago

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 this callback function.

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