我是 React 的新手。状态数组在排序功能后仅更新一次。为什么第二次触发排序功能后没有再次更新?
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)
}