export const useStateRef = <S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>, MutableRefObject<S>] => {
const [state, setState] = useState(initialState);
const stateRef = useRef(state);
const setStateHandle = useCallback((val: S | ((pre?: S) => S)) => {
if (typeof val === 'function') {
const temp = val as (pre?: S) => S;
stateRef.current = temp(stateRef.current);
} else {
stateRef.current = val;
}
setState(val);
}, []);
return [state, setStateHandle as Dispatch<SetStateAction<S>>, stateRef];
};