refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
28 lines (24 loc) • 904 B
JavaScript
import { useRef, useLayoutEffect } from 'react';
import { EMPTY_OBJECT, NOOP, EMPTY_ARRAY } from 'tsfn';
import { shallowEqualByKeys } from './utils';
export var onLayout = function onLayout(onLayoutHandler, watchKeys) {
return function (props) {
var propsRef = useRef(EMPTY_OBJECT);
var useEffectFnRef = useRef(NOOP);
var watchValuesRef = useRef(EMPTY_ARRAY);
if (watchValuesRef.current === EMPTY_ARRAY || !shallowEqualByKeys(propsRef.current, props, watchKeys)) {
watchValuesRef.current = watchKeys.map(function (k) {
return props[k];
});
}
propsRef.current = props;
if (useEffectFnRef.current === NOOP) {
useEffectFnRef.current = function () {
return onLayoutHandler(propsRef.current);
};
}
useLayoutEffect(useEffectFnRef.current, watchValuesRef.current);
return props;
};
};
//# sourceMappingURL=on-layout.js.map