react-use/src/useGetSet.ts
streamich 395e82b03b feat: 🎸 improve useCounter interface
BREAKING CHANGE: useCounter interface changed
2018-10-29 18:42:47 +01:00

17 lines
421 B
TypeScript

import {useRef, useCallback} from './react';
import useUpdate from './useUpdate';
const useGetSet = <T>(initialValue: T): [() => T, (value: T) => void] => {
let state = useRef(initialValue);
const update = useUpdate();
const get = useCallback(() => state.current, []);
const set = useCallback((value: T) => {
state.current = value;
update();
}, []);
return [get, set];
};
export default useGetSet;