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

1 Answer

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({ name: 'John' }, () => console.log('The name has updated and component re-rendered'))

Related questions

0 votes
    How to pass a parameter to an event handler or callback in REACT?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    When to use a Class Component over a Function Component?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    How to bind methods or event handlers in JSX callbacks?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the difference between HTML and React event handling?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    Why should we not update the state directly in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the difference between state and props in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What are props in React?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is state in React JS?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What are Pure Components in React js1?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is the difference between Element and Component?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    What is JSX?...
asked Jun 2, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 7, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    Write a program in Java to find the area and perimeter of a right- angled triangle by using function ... inputs. angled triangle Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    Let's say I have a JSON set of events, each with a start time and end time, and I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
...