mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
More DRY code. Also move non-hooks to separate directories. BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint` BREAKING CHANGE: `comps` directory renamed to `component`
22 lines
632 B
TypeScript
22 lines
632 B
TypeScript
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();
|
|
},
|
|
],
|
|
[]
|
|
);
|
|
}
|