File size: 632 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import { Dispatch, useMemo, useRef } from 'react';
import useUpdate from './useUpdate';
import { IHookStateInitAction, IHookStateSetAction, resolveHookState } from './misc/hookState';
export default function useGetSet<S>(
initialState: IHookStateInitAction<S>
): [get: () => S, set: Dispatch<IHookStateSetAction<S>>] {
const state = useRef(resolveHookState(initialState));
const update = useUpdate();
return useMemo(
() => [
() => state.current as S,
(newState: IHookStateSetAction<S>) => {
state.current = resolveHookState(newState, state.current);
update();
},
],
[]
);
}
|