Created
November 26, 2021 10:13
-
-
Save niikkiin/85a37f3ce95c98a5410d9ab49fd3ff65 to your computer and use it in GitHub Desktop.
Implement loading screen on Nextjs
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 { AppProps } from 'next/app'; | |
import { ThemeProvider } from 'next-themes'; | |
import { useRouter } from 'next/router'; | |
import { useState, useEffect } from 'react'; | |
// styles | |
import 'tailwindcss/tailwind.css'; | |
import '@/styles/tailwind.css'; | |
import '@/styles/globals.scss'; | |
function App({ Component, pageProps }: AppProps) { | |
const router = useRouter(); | |
const [pageLoading, setPageLoading] = useState<boolean>(false); | |
useEffect(() => { | |
const handleStart = () => { | |
setPageLoading(true); | |
}; | |
const handleComplete = () => { | |
setPageLoading(false); | |
}; | |
router.events.on('routeChangeStart', handleStart); | |
router.events.on('routeChangeComplete', handleComplete); | |
router.events.on('routeChangeError', handleComplete); | |
}, [router]); | |
return ( | |
<ThemeProvider attribute='class'> | |
{pageLoading ? ( | |
<div className='bg-main-dark h-screen w-screen flex items-center justify-center text-5xl'> | |
{' '} | |
Loading... | |
</div> | |
) : ( | |
<> | |
<Component {...pageProps} /> | |
</> | |
)} | |
</ThemeProvider> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment