react-use/src/useGetSet.ts
2019-04-12 23:18:11 +10:00

17 lines
423 B
TypeScript

import { useCallback, useRef } from 'react';
import useUpdate from './useUpdate';
const useGetSet = <T>(initialValue: T): [() => T, (value: T) => void] => {
const 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;