UNPKG

@wordpress/components

Version:
42 lines (39 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useOnValueUpdate = useOnValueUpdate; var _compose = require("@wordpress/compose"); var _element = require("@wordpress/element"); /* eslint-disable jsdoc/require-param */ /** * WordPress dependencies */ /** * Context object for the `onUpdate` callback of `useOnValueUpdate`. */ /** * Calls the `onUpdate` callback when the `value` changes. */ function useOnValueUpdate( /** * The value to watch for changes. */ value, /** * Callback to fire when the value changes. */ onUpdate) { const previousValueRef = (0, _element.useRef)(value); const updateCallbackEvent = (0, _compose.useEvent)(onUpdate); (0, _element.useLayoutEffect)(() => { if (previousValueRef.current !== value) { updateCallbackEvent({ previousValue: previousValueRef.current }); previousValueRef.current = value; } }, [updateCallbackEvent, value]); } /* eslint-enable jsdoc/require-param */ //# sourceMappingURL=use-on-value-update.js.map