@wordpress/components
Version:
UI components for WordPress.
19 lines (18 loc) • 665 B
JavaScript
import { useEvent } from "@wordpress/compose";
import { useLayoutEffect, useRef } from "@wordpress/element";
// packages/components/src/utils/hooks/use-on-value-update.ts
/**
* Calls the `onUpdate` callback when the `value` changes.
*/
function useOnValueUpdate(value, onUpdate) {
const previousValueRef = useRef(value);
const updateCallbackEvent = useEvent(onUpdate);
useLayoutEffect(() => {
if (previousValueRef.current !== value) {
updateCallbackEvent({ previousValue: previousValueRef.current });
previousValueRef.current = value;
}
}, [updateCallbackEvent, value]);
}
export { useOnValueUpdate };
//# sourceMappingURL=use-on-value-update.mjs.map