UseState and API Requests
Answered
LeonTM posted this in #help-forum
LeonTMOP
In the picture you see a fetchData function that I am calling on component load with a useEffect Hook on a client component. The api response is defined as data. I am checking if it has the data property and return if not. If it is defined it always is an array of tasks. This array is saved inside of the arr variable. tasks and orgTasks is set to a reversed version of this exact array. For some reason, it sets it to arr, so non-reversed. It is just the reversed version when I console.log the reversed version first. Why is this and or is there a fix to it?
Answered by chisto
i think .reverse mutates the original array
so in the furst console log you reverse it, then again on setTasks, then again on setOrgTasks
so in the furst console log you reverse it, then again on setTasks, then again on setOrgTasks