转载请注明出处: http://qiudeqing.com/react/2023/07/17/react-hook.html

自定义hook

useArray

const {value, clear, removeIndex, add} = useArray([])

const useArray = <T>(initialArray: T[]) => {
  const [value, setValue] = useState(initialArray)

  return {
    value,
    setValue,
    add: (item: T) => setValue([...value, item]),
    clear: () => setValue([]),
    removeIndex: (index: number) => {
      const copy = [...value]
      copy.splice(index, 1)
      setValue(copy)
    }
  }
}