Created
December 16, 2022 18:24
-
-
Save chamerling/6c5a11850570961c7c2e66890bce263b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ComponentType, useEffect, useRef } from "react"; | |
const withWebPerformance = < | |
T extends object & { onLoaded?: (measure: PerformanceMeasure) => void } | |
>( | |
Component: ComponentType<T>, | |
name: string | |
) => { | |
return (props: T) => { | |
const { onLoaded } = props; | |
const ref = useRef(0); | |
useEffect(() => { | |
ref.current++; | |
}); | |
useEffect(() => { | |
if (ref.current === 1) { | |
return; | |
} | |
console.log(performance.mark(name)); | |
return () => { | |
const measure = performance.measure(name, name); | |
console.log(measure); | |
onLoaded?.(measure); | |
}; | |
}, [onLoaded]); | |
return <Component {...props} />; | |
}; | |
}; | |
export { withWebPerformance }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment