UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

35 lines (34 loc) 1.62 kB
import { useEffect, useRef } from 'react'; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuidv4 from 'uuid/v4'; import { getPropsDifference, getShallowPropsDifference } from '../../../compare-props'; export function useComponentRenderTracking({ onRender, propsDiffingOptions, zeroBasedCount = true }) { const propsRef = useRef(); const renderCountRef = useRef(zeroBasedCount ? 0 : 1); // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead const { current: componentId } = useRef(uuidv4()); useEffect(() => { const lastProps = propsRef.current; const renderCount = renderCountRef.current; let propsDifference; if (propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.enabled && lastProps) { propsDifference = propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.useShallow ? getShallowPropsDifference(lastProps, propsDiffingOptions.props) : getPropsDifference(lastProps, propsDiffingOptions.props, 0, 2, propsDiffingOptions === null || propsDiffingOptions === void 0 ? void 0 : propsDiffingOptions.propsToIgnore); } const result = { renderCount, propsDifference, componentId }; onRender(result); if (propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.enabled) { propsRef.current = propsDiffingOptions.props; } renderCountRef.current = renderCountRef.current + 1; }); // No dependencies run on each render }