UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

27 lines (24 loc) 881 B
import { useRef, useEffect } from 'react'; import { EMPTY_OBJECT, NOOP, EMPTY_ARRAY } from 'tsfn'; import { shallowEqualByKeys } from './utils'; export var onUpdate = function onUpdate(onUpdateFn, watchKeys) { return function (props) { var useEffectFnRef = useRef(NOOP); var propsRef = useRef(EMPTY_OBJECT); var watchValuesRef = useRef(EMPTY_ARRAY); if (watchValuesRef.current === EMPTY_ARRAY || !shallowEqualByKeys(propsRef.current, props, watchKeys)) { watchValuesRef.current = watchKeys.map(function (k) { return props[k]; }); } if (useEffectFnRef.current === NOOP) { useEffectFnRef.current = function () { return onUpdateFn(propsRef.current); }; } propsRef.current = props; useEffect(useEffectFnRef.current, watchValuesRef.current); return props; }; }; //# sourceMappingURL=on-update.js.map