0 votes
in ReactJS by

What is the second argument that can optionally be passed to setState and what is its purpose?

1 Answer

0 votes
by
edited by

A callback work which will be conjured when setState has completed and the part is re-rendered. 
Something that is not talked about a great deal is that setState is asynchronous, which is the reason it takes in a moment callback function. Ordinarily it's best to utilize another lifecycle strategy instead of depending on this callback function, however, it's great to know it exists.

1

2

3

4

5

6

7

8

9

10

11

Class Training extends Course

{

this.state = {

sampleItem: 'learn',

}

handleChange = (event) => {

console.log(this.state.sampleItem)

this.setState({

sampleItem: event.target.value      //event.target.value = Welcome

}, () => console.log(this.state.sampleItem))

};

Output:

Learn

Welcome

Related questions

0 votes
asked Jul 2, 2019 in ReactJS by Venkatshastri
0 votes
asked Jul 2, 2019 in ReactJS by Venkatshastri
...