mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {Profiler} from 'react'
2
3// Don't let it get stripped out in profiling builds (which apply production Babel preset).
4const log = (global as any)['con' + 'sole'].log
5
6function onRender(id: string, phase: string, actualDuration: number) {
7 if (!__DEV__) {
8 // This block of code will exist in the production build of the app.
9 // However, only profiling builds of React call `onRender` so it's dead code in actual production.
10 const message = `<Profiler> ${id}:${phase} ${
11 actualDuration > 500
12 ? '(╯°□°)╯ '
13 : actualDuration > 100
14 ? '[!!] '
15 : actualDuration > 16
16 ? '[!] '
17 : ''
18 }${Math.round(actualDuration)}ms`
19 log(message)
20 }
21}
22
23export function AppProfiler({children}: {children: React.ReactNode}) {
24 return (
25 <Profiler id="app" onRender={onRender}>
26 {children}
27 </Profiler>
28 )
29}