Skip to content
Advertisement

How can I render array after sorting in React hooks

I get by props array with objects and then I nedd to sort them and render, but now I get array and after sorting it I can’t render sorted array. I use useEffect and there sorting an array and by setState put sorted array into variable but when I try to rended it I have error that array are empty. How can I fix it? Maybe I can somehow change props.friendsList with newFriends? it will be great!

JavaScript

console.log (friends) show at first empty array and then fill

Advertisement

Answer

I think it would be better just to sort friends directly. The useEffect and state are unnecessary. To keep this optimised you should use useMemo, but you need to make sure props.friendsList is not changing on every render:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement