Skip to content

Instantly share code, notes, and snippets.

@niikkiin
Created November 26, 2021 10:13
Show Gist options
  • Save niikkiin/85a37f3ce95c98a5410d9ab49fd3ff65 to your computer and use it in GitHub Desktop.
Save niikkiin/85a37f3ce95c98a5410d9ab49fd3ff65 to your computer and use it in GitHub Desktop.
Implement loading screen on Nextjs
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