import {Dispatch, SetStateAction, useCallback, useRef, useState} from 'react'; import {EMPTY_ARRAY} from 'default-values'; type Ref = {readonly current: T}; // @ts-ignore function useStateRef(): [ S | undefined, Dispatch>, Ref, ]; function useStateRef(initialState: S | (() => S)): [S, Dispatch>, Ref]; function useStateRef(initialState: S | (() => S) | undefined) { const [state, setState] = useState(initialState); const ref = useRef(state); const dispatch = useCallback( (val: SetStateAction) => { ref.current = typeof val === 'function' ? (val as any)(ref.current) : val; setState(ref.current); }, EMPTY_ARRAY, // eslint-disable-line react-hooks/exhaustive-deps ); return [state, dispatch, ref]; } export default useStateRef;