chore: optim

This commit is contained in:
lencx
2022-12-19 23:09:17 +08:00
parent 8966ebbd03
commit 47a3bace5b
9 changed files with 142 additions and 56 deletions

12
src/hooks/useData.ts vendored
View File

@@ -7,9 +7,8 @@ export default function useData(oData: any[]) {
const [opData, setData] = useState<any[]>([]);
useEffect(() => {
const nData = oData.map(i => ({ [safeKey]: v4(), ...i }));
setData(nData);
}, [oData])
opInit(oData);
}, [])
const opAdd = (val: any) => {
const v = [val, ...opData];
@@ -17,6 +16,11 @@ export default function useData(oData: any[]) {
return v;
};
const opInit = (val: any[] = []) => {
const nData = val.map(i => ({ [safeKey]: v4(), ...i }));
setData(nData);
};
const opRemove = (id: string) => {
const nData = opData.filter(i => i[safeKey] !== id);
setData(nData);
@@ -31,5 +35,5 @@ export default function useData(oData: any[]) {
return nData;
};
return { opSafeKey: safeKey, opReplace, opAdd, opRemove, opData };
return { opSafeKey: safeKey, opInit, opReplace, opAdd, opRemove, opData };
}