React的useCallback没有按预期工作,依赖项sortRuleList未更新,问题出在哪里?-灵析社区

兰豆儿

const [sortRuleList, setSortRuleList] = useState([]); const newsortRule = () => { setSortRuleList(prevList => { const newList = [...prevList, { sortTypeCode: '', sortType: '', orderType: '' }]; setTimeout(() => { console.log("Updated sortRuleList:", newList); ref.current.reload(); }, 0); return newList; }); }; const onCellChange = useCallback((value: any, type: any, idx: any) => { setSortRuleList(prevList => { const newList = [...prevList]; if (type === 'sortTypeCode') { newList[idx]['sortTypeCode'] = value; sortbyArr.forEach(ele => { if (ele.value == value) { newList[idx]['sortType'] = ele.label; } }); } if (type === 'orderType') { newList[idx]['orderType'] = value; } return newList; }); setTimeout(() => { // 这里用最新 sortRuleList 值 setSortRuleList(prevList => { console.log("909090", prevList); return prevList; // 日志 }); }, 0); }, [sortbyArr]);

阅读量:1

点赞量:0

问AI