refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
33 lines (24 loc) • 919 B
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onLayout = void 0;
var _react = require("react");
var _tsfn = require("tsfn");
var _utils = require("./utils");
const onLayout = (onLayoutHandler, watchKeys) => props => {
const propsRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT);
const useEffectFnRef = (0, _react.useRef)(_tsfn.NOOP);
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]);
}
propsRef.current = props;
if (useEffectFnRef.current === _tsfn.NOOP) {
useEffectFnRef.current = () => {
return onLayoutHandler(propsRef.current);
};
}
(0, _react.useLayoutEffect)(useEffectFnRef.current, watchValuesRef.current);
return props;
};
exports.onLayout = onLayout;