import { useState } from '@tarojs/taro';

const useArrayValue = <T>(initArr: T[]): [T[], (newValue: T) => void] => {
  const [arr, setArr] = useState(initArr);
  const getValueHandle = (newValue: T) => {
    let index = arr.indexOf(newValue);
    if (index === -1) {
      setArr([...arr, newValue]);
    } else {
      setArr([...arr.slice(0, index), ...arr.slice(index + 1)]);
    }
  };
  return [arr, getValueHandle];
};

export default useArrayValue;
