UNPKG

@wordpress/components

Version:
1 lines • 1.33 kB
{"version":3,"file":"use-update-effect.mjs","names":[],"sources":["../../../src/utils/hooks/use-update-effect.ts"],"sourcesContent":["import type { DependencyList, EffectCallback } from 'react';\nimport { useRef, useEffect } from '@wordpress/element';\n\n/**\n * A `React.useEffect` that will not run on the first render.\n * Source:\n * https://github.com/ariakit/ariakit/blob/main/packages/ariakit-react-core/src/utils/hooks.ts\n *\n * @param effect\n * @param deps\n */\nfunction useUpdateEffect( effect: EffectCallback, deps: DependencyList ) {\n\tconst mountedRef = useRef( false );\n\tuseEffect( () => {\n\t\tif ( mountedRef.current ) {\n\t\t\treturn effect();\n\t\t}\n\t\tmountedRef.current = true;\n\t\treturn undefined;\n\t\t// 1. This hook needs to pass a dep list that isn't an array literal\n\t\t// 2. `effect` is missing from the array, and will need to be added carefully to avoid additional warnings\n\t\t// see https://github.com/WordPress/gutenberg/pull/41166\n\t}, deps );\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tmountedRef.current = false;\n\t\t},\n\t\t[]\n\t);\n}\n\nexport default useUpdateEffect;\n"],"mappings":";;;;;;;;;;AAWA,SAAS,gBAAiB,QAAwB,MAAuB;CACxE,MAAM,aAAa,OAAQ,KAAM;CACjC,gBAAiB;EAChB,IAAK,WAAW,SACf,OAAO,OAAO;EAEf,WAAW,UAAU;CAKtB,GAAG,IAAK;CAER,sBACa;EACX,WAAW,UAAU;CACtB,GACA,CAAC,CACF;AACD"}