UNPKG

refun

Version:

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

32 lines (24 loc) 902 B
Object.defineProperty(exports, "__esModule", { value: true }); exports.onUpdate = void 0; var _react = require("react"); var _tsfn = require("tsfn"); var _utils = require("./utils"); const onUpdate = (onUpdateFn, watchKeys) => props => { const useEffectFnRef = (0, _react.useRef)(_tsfn.NOOP); const propsRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const watchValuesRef = (0, _react.useRef)(_tsfn.EMPTY_ARRAY); if (watchValuesRef.current === _tsfn.EMPTY_ARRAY || !(0, _utils.shallowEqualByKeys)(propsRef.current, props, watchKeys)) { watchValuesRef.current = watchKeys.map(k => props[k]); } if (useEffectFnRef.current === _tsfn.NOOP) { useEffectFnRef.current = () => { return onUpdateFn(propsRef.current); }; } propsRef.current = props; (0, _react.useEffect)(useEffectFnRef.current, watchValuesRef.current); return props; }; exports.onUpdate = onUpdate;