import { useState, useRef, useEffect } from "react"; import type { Dispatch, SetStateAction } from "react"; type GetStateAction = () => S; export default function useGetState(initValue: S | (() => S)): [S, Dispatch>, GetStateAction] { const [state, setState] = useState(initValue); const ref = useRef(); useEffect(() => { ref.current = state; }, [state]); const getState = (): S => ref.current; // ?? 源码加了useCallback return [state, setState, getState]; }