Next.js Discord

Discord Forum

UseState and API Requests

Answered
LeonTM posted this in #help-forum
Open in Discord
Avatar
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?
Image
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
View full answer

3 Replies

Avatar
Might not be a nextjs problem or question, but outside nextjs it works fine
Avatar
i think .reverse mutates the original array
so in the furst console log you reverse it, then again on setTasks, then again on setOrgTasks
Answer
Avatar
Okay, thats weird. Works fine now thanks a lot.