@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
32 lines • 1.43 kB
JavaScript
import { useEffect, useRef } from 'react';
import { uuid } from '@atlaskit/adf-schema';
import { getPropsDifference, getShallowPropsDifference } from '../../../compare-props';
export function useComponentRenderTracking({
onRender,
propsDiffingOptions,
zeroBasedCount = true
}) {
const propsRef = useRef();
const renderCountRef = useRef(zeroBasedCount ? 0 : 1);
const {
current: componentId
} = useRef(uuid.generate());
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
}