请教react hook的问题,主要目的就是实现数组里面的字符,增加和删除。
因为新增的问题还没解决,所以删除不知道这样写对不对!
这里的数组是不是set哪里写错了?欢迎指教
const [checkListBtn, setCheckListBtn] = useState<string[]>([])//待选择列表
const treeChange = (data: string) =>{
const uniqueIds: any[] = [];
let isDo: boolean = false;
setCheckListBtn(current => { //数组删除
return current.filter(element => {
const isDuplicate = uniqueIds.includes(data);
if (isDuplicate) {
isDo = true; //如果删除成功则不进入增加方法
return false;
}
return true;
});
});
if(!isDo){ //如果没有删除,则进入增加元素
let arr = [...checkListBtn,data]
setCheckListBtn(arr) //这里set不生效,验证过arr是已经有值的
}
}
