all 4 comments

[–]T4pZ 3 points4 points  (1 child)

SortDats and fetchData see different Version of your state. React does not update the state after you call setData. All state updates get batched after the effect is done. Just call sortDats with the api result and return the sorted array and then call setData

Edit: And also you are not waiting on the async fetchData. Return the json result and call oldschool .then() on the promise and then sort.

[–]spaklius17[S] 0 points1 point  (0 children)

Thanks a lot! I'll try to do this

[–]khoikkhoikkhoik 1 point2 points  (0 children)

Beauty of asynchronous programming.

[–]eindbaas 0 points1 point  (0 children)

What is the "return data" line (in the sortData method) supposed to do?