Skip to content

Commit dd98a8e

Browse files
committed
添加一个泛型useArray
1 parent a1e3e1b commit dd98a8e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/utils/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ export const useDebounce = <V>(value: V, delay?: number) => {
4545
}, [value, delay])
4646

4747
return debouncedValue
48+
}
49+
50+
51+
//编写一个泛型
52+
export const useArray = <T>(initialArray:T[]) =>{
53+
const [value, setValue] = useState(initialArray)
54+
return{
55+
value,
56+
setValue,
57+
add: (item:T) => setValue([...value, item]),
58+
clear: ()=> setValue([]),
59+
removeIndex: (index: number) =>{
60+
const copy = [...value]
61+
copy.splice(index,1)
62+
setValue(copy)
63+
}
64+
}
4865
}

0 commit comments

Comments
 (0)