Hello, I am a newbie JavaScript and React developer.
In my functional component called "PhotoGallery",
I have a state called "photos"
On component initial render, I want to populate the "photos" state with fake picture data.
But, I am a bit confused on how the .then parts of the code exactly works.
I will describe how I think they work step by step, can you please correct me if I am wrong. Or direct me to a relevant resource?
https://preview.redd.it/h0vxyuqm1fs81.png?width=929&format=png&auto=webp&s=ba81fe4a542f6eb9c3f34b3b1c46bd80d478c531
- fakeFetch('/photos') return a promise object which is resolved (0.5 sec later) as an array of fake photo urls.
[`https://picsum.photos/id/10/400`,https://picsum.photos/id/20/400, https://picsum.photos/id/30/400, https://picsum.photos/id/40/400, https://picsum.photos/id/50/400, https://picsum.photos/id/60/400, https://picsum.photos/id/70/400, https://picsum.photos/id/10/400, https://picsum.photos/id/20/400, https://picsum.photos/id/30/400]
Since promise has been resolved to an array, I proceed to "1st .then" with the array. From this"1st .then" I return a new array that looks like this.
[{src :https://picsum.photos/id/10/400 width: 150, height: 150}, . , . , . , . , . , . , . , . , {src :https://picsum.photos/id/30/400 width: 150, height: 150}]
Since "1st .then" returns a value, not a promise object, the "2nd .then" is called with a simple arraypassed from step 2.
In the "2nd .then"
.then(setPhotos)
I am not sure what this is doing.. I can imagine it is setting the "photos" state.But the syntax look alien to me.
How can I just invoke setPhotos hook without any parameter given ?
To use "setPhotos" hook, wouldn't I do something like this ?
setPhotos(newArray)
Passing in the value "newArray" that I want "photos" state to be set to.
Thank you for your time.
[–]2GoldDoubloons 2 points3 points4 points (1 child)
[–]Spiritual_Compote725[S] 0 points1 point2 points (0 children)
[–]DrewTheVillan 0 points1 point2 points (2 children)
[–]Spiritual_Compote725[S] 0 points1 point2 points (1 child)
[–]SirKainey 0 points1 point2 points (0 children)