import { Dispatch, MutableRefObject, SetStateAction, useEffect, useRef, useState } from 'react' export type CurrentStateType = [ S, Dispatch>, MutableRefObject ] const useCurrentState = (initialState: S): CurrentStateType => { const [state, setState] = useState(initialState as S) const ref = useRef(initialState as S) useEffect(() => { ref.current = state }, [state]) const setValue = (val: S) => { ref.current = val setState(val) } return [state, setValue, ref] } export default useCurrentState