React的useCallback没起作用,是怎么回事?加了依赖项sortRuleList了,而sortRuleList在newsortRule函数里先行调用了,没啥问题啊,为什么打印出来还是空?
const [sortRuleList, setSortRuleList] = useState<any>([])
const newsortRule = ()=>{
const arr = [...sortRuleList]
arr.push({
sortTypeCode:'',
sortType:'',
orderType:''
})
setTimeout(()=>{
setSortRuleList(arr)
ref.current.reload()
},0)
}
const onCellChange = useCallback((value: any, type:any, idx: any) => {
console.log("888888",sortRuleList) //为什么打印出来是空
const arr = [...sortRuleList] //为什么打印出来是空
console.log("99999",arr)
if(type == 'sortTypeCode'){
arr[idx]['sortTypeCode'] = value
sortbyArr.forEach((ele) => {
if (ele.value == value) {
arr[idx]['sortType'] = ele.label
}
})
}
if(type == 'orderType'){
arr[idx]['orderType'] = value
}
setSortRuleList(arr)
},[sortRuleList])
自己一直在调试,没成功。
再问楼下回答的大佬,没成功,打印出来,909090 undefined