I am new to React. The array in state is updating only once after sort function. Why is it not updated again after the sort function is triggered second time?
const [cases, setCases] = useState([1, 2, 3, 4, 5]);
let sortDown = true
let sorted = []
function sort(){
const copy = [...cases]
if(sortDown){
sorted = copy.sort(function(a, b){
return b - a
})
} else {
sorted = copy.sort(function(a, b){
return a - b
})
}
sortDown = !sortDown
setCases(sorted)
}
sort()copyarray is sorted. So usesetCases(copy)instead.