import { useEffect, useRef, useState } from 'react';

/**
 * 生成具有初始值的状态引用
 *
 * @param initialValue - 状态的初始值
 * @return 包含状态值、状态设置函数和可变引用对象的数组
 */
export function useStateRef<T>(initialValue: T) {
  const [value, setValue] = useState(initialValue);

  const ref = useRef(value);

  useEffect(() => {
    ref.current = value;
  }, [value]);

  return [value, setValue, ref] as const;
}
