交作业啦
来源:4-6 作业练习 - 用 Hook + TS + TS泛型实现useArray

骑猪跑的程序员
2021-10-27
export const useArray = <P>(persons: P[]) => {
// hello,请把作业写在这里吧,写完记得再对照作业要求检查一下
const [personList, setPersonList] = useState<P[]>(persons);
return {
value: personList,
clear: () => {
setPersonList([]);
},
removeIndex: (index: number) => {
// 根据指定index 删除
const newPerson = [...personList];
newPerson.splice(index, 1);
console.log(newPerson, personList);
setPersonList(newPerson);
},
add: (item: P) => { setPersonList([ ...personList, item]) }
}
}
写回答
1回答
-
Nolan
2021-11-14
very good
00
相似问题