refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
37 lines (29 loc) • 1.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onUpdate = void 0;
var _react = require("react");
var _tsfn = require("tsfn");
var _utils = require("./utils");
var onUpdate = function onUpdate(onUpdateFn, watchKeys) {
return function (props) {
var useEffectFnRef = (0, _react.useRef)(_tsfn.NOOP);
var propsRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT);
var watchValuesRef = (0, _react.useRef)(_tsfn.EMPTY_ARRAY);
if (watchValuesRef.current === _tsfn.EMPTY_ARRAY || !(0, _utils.shallowEqualByKeys)(propsRef.current, props, watchKeys)) {
watchValuesRef.current = watchKeys.map(function (k) {
return props[k];
});
}
if (useEffectFnRef.current === _tsfn.NOOP) {
useEffectFnRef.current = function () {
return onUpdateFn(propsRef.current);
};
}
propsRef.current = props;
(0, _react.useEffect)(useEffectFnRef.current, watchValuesRef.current);
return props;
};
};
exports.onUpdate = onUpdate;
//# sourceMappingURL=on-update.js.map