UNPKG

@wordpress/components

Version:
8 lines (7 loc) 1.35 kB
{ "version": 3, "sources": ["../../../src/utils/hooks/use-on-value-update.ts"], "sourcesContent": ["/* eslint-disable jsdoc/require-param */\n/**\n * WordPress dependencies\n */\nimport { useEvent } from '@wordpress/compose';\nimport { useRef, useLayoutEffect } from '@wordpress/element';\n\n/**\n * Context object for the `onUpdate` callback of `useOnValueUpdate`.\n */\n\n/**\n * Calls the `onUpdate` callback when the `value` changes.\n */\nexport function useOnValueUpdate(\n/**\n * The value to watch for changes.\n */\nvalue,\n/**\n * Callback to fire when the value changes.\n */\nonUpdate) {\n const previousValueRef = useRef(value);\n const updateCallbackEvent = useEvent(onUpdate);\n useLayoutEffect(() => {\n if (previousValueRef.current !== value) {\n updateCallbackEvent({\n previousValue: previousValueRef.current\n });\n previousValueRef.current = value;\n }\n }, [updateCallbackEvent, value]);\n}\n/* eslint-enable jsdoc/require-param */"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,qBAAyB;AACzB,qBAAwC;AASjC,SAAS,iBAIhB,OAIA,UAAU;AACR,QAAM,uBAAmB,uBAAO,KAAK;AACrC,QAAM,0BAAsB,yBAAS,QAAQ;AAC7C,sCAAgB,MAAM;AACpB,QAAI,iBAAiB,YAAY,OAAO;AACtC,0BAAoB;AAAA,QAClB,eAAe,iBAAiB;AAAA,MAClC,CAAC;AACD,uBAAiB,UAAU;AAAA,IAC7B;AAAA,EACF,GAAG,CAAC,qBAAqB,KAAK,CAAC;AACjC;", "names": [] }