@shopify/polaris
Version:
Shopify’s product component library
32 lines (27 loc) • 893 B
JavaScript
import { useRef } from 'react';
import { useIsAfterInitialMount as useIsAfterInitialMount$1 } from './use-is-after-initial-mount.js';
/**
* Similarly to the life-cycle method componentDidMount, useComponentDidMount
* will be invoked after the component has mounted, and only the initial mount.
* @param callback Defines a callback to invoke once the component has
* initially mounted.
* @example
* function Playground({active}) {
* useComponentDidMount(() => {
* if (active) {
* console.warning(`Component has mounted.`);
* }
* });
*
* return null;
* }
*/
function useComponentDidMount(callback) {
var isAfterInitialMount = useIsAfterInitialMount$1();
var hasInvokedLifeCycle = useRef(false);
if (isAfterInitialMount && !hasInvokedLifeCycle.current) {
hasInvokedLifeCycle.current = true;
return callback();
}
}
export { useComponentDidMount };