Created
November 18, 2021 03:24
-
-
Save ScriptedAlchemy/115f4f03a586effc67d76d636aea9ad9 to your computer and use it in GitHub Desktop.
Recursive Measure Render
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
function recursiveMap(children, fn) { | |
return React.Children.map(children, (child) => { | |
if (!React.isValidElement(child)) { | |
return child; | |
} | |
if (child.props.children) { | |
child = React.cloneElement(child, { | |
children: recursiveMap(child.props.children, fn), | |
}); | |
} | |
return fn(child); | |
}); | |
} | |
const RecursiveMeasureRender = ({ children, postfix = '' }) => { | |
const fn = (el) => { | |
const componentName = el?.type?.displayName || el?.type?.name; | |
if (componentName && !componentName?.includes?.('LazyHydrate') && !componentName?.includes?.('MeasureRender')) { | |
// if (!el?.type?.displayName) console.log('missing displayName', el?.type?.name); | |
return ( | |
<MeasureRender name={(el.type.displayName || el.type.name) + postfix}> | |
{React.isValidElement(el) && el} | |
</MeasureRender> | |
); | |
} | |
return el; | |
}; | |
if (process.browser) { | |
if (window.withPerf) { | |
return <>{recursiveMap(children, fn)}</>; | |
} | |
} | |
return children; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment