score:0

the concept of that example is not about fetch , the example explains that even if you call two set states within a same function , the variables present in the set state are completely independent.but when the state is updated they are merge to a single update.

score:4

state update are merged means that when you update only one key in the object state it will not affect the other keys.

for example let's say your state is as followed:

this.state = { key1 : value1, key2 : value2 }

if you use this.setstate such as :

this.setstate({ key1 : value3 }

your new state will be :

this.state = { key1 : value3, key2 : value2 }

it has merged the update with the existing state, meaning it did not affect the key2. if state update would not be merged the new state would be

this.state = { key1 : value 3}

and key2 would not exist anymore in your state as it was not defined in your update.

the merging affects key/value pair which are not included in your update.

also it means that if you have different this.setstate({}) in the same function, all the update will be gathered together and the state update will happen only once.

in the example from the doc two different fetch functions are used because each of them has a different role, the first one is fetching posts, therefor the setstate update the posts (and do not affect the this.state.comments as "state update are merged") and the second one is fetching comments, updating the state with the new comments and not affecting the this.state.posts value.

edit: the response argument is the response you get from the function fetch, which is normally fetching data from a source (your database, an external api...)


Related Query

More Query from same tag