score:0

Accepted answer

maybe you can try put allques state on the array dependencies of second useeffect parameter, because it make watch the state which is allques, then when allques changed so component do side effect on first parameter of useeffect()

useeffect(() => {
    fetch('http://localhost:4000/questions')
        .then(res => {
            return res.json();
        })
        .then(data => {
            if (sortway === 'latest') {
                data.sort((a, b) => b.sortorder - a.sortorder);
            }
            setallques(data);
        });
}, [allques]);

score:2

don't directly sort sortarr1, first of all, make its copy and then sort that copy and then set sorted array into the state.

you can copy array as below:

let copiedarr = [...sortarr1];
copiedarr.sort(sortfn);

setsortarr1(copiedarr);

Related Query

More Query from same tag