Categories

Nov 26, 2019 in React JS
Q:

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

1 Answer

Nov 26, 2019

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

Click here to read more about React JS
Click here to read more about Insurance

Related questions

Madanswer
Jul 2, 2019 in React JS
Jul 2, 2019 in React JS
Nov 26, 2019 in React JS
...