Created
September 29, 2023 17:45
-
-
Save MarlonPassos-git/438bca73176559fc98480d5dd55cd7ca to your computer and use it in GitHub Desktop.
lazy Component
This file contains 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 type { FC, ReactElement } from 'react' | |
import { useEffect, useState } from 'react' | |
interface DomContentInterface { | |
children: ReactElement<any, any> | null | |
} | |
const DOMContentLoadedWrapper: FC<DomContentInterface> = ({ children }) => { | |
const [isReady, setIsReady] = useState(false) | |
useEffect(() => { | |
const handleDOMContentLoaded = () => { | |
setIsReady(true) | |
} | |
if (document.readyState === 'complete') { | |
setIsReady(true) | |
} else { | |
document.addEventListener('DOMContentLoaded', handleDOMContentLoaded) | |
} | |
return () => { | |
document.removeEventListener('DOMContentLoaded', handleDOMContentLoaded) | |
} | |
}, []) | |
if (!children) { | |
return <></> | |
} | |
return isReady ? children : <></> | |
} | |
export default DOMContentLoadedWrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment