Skip to content

Instantly share code, notes, and snippets.

@KATT
Last active June 30, 2022 11:35
Show Gist options
  • Save KATT/13a59f3cdd443f53017694dcf3511dcd to your computer and use it in GitHub Desktop.
Save KATT/13a59f3cdd443f53017694dcf3511dcd to your computer and use it in GitHub Desktop.
Prevent Flash of Unstyled Content in Chrome
// Next.js' `_app.js` file
import Head from 'next/head';
import { useEffect, useState } from 'react';
import '../styles/main.scss';
function useIsMounted() {
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
return mounted;
}
function PreventFlashOfUnstyledContent() {
const mounted = useIsMounted();
if (mounted) {
return null;
}
return (
<Head>
<style
id="preventFlashOfUnstyledContent"
dangerouslySetInnerHTML={{
__html: `*, *::before, *::after { transition: none !important; }`,
}}
/>
</Head>
);
}
const App = ({ Component, pageProps }) => {
return (
<>
<PreventFlashOfUnstyledContent />
<Component {...pageProps} />
</>
);
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment