Skip to content

Instantly share code, notes, and snippets.

@acorcutt
Last active June 23, 2021 12:04
Show Gist options
  • Save acorcutt/1934c85c852a0d2e8dff32066b0b0783 to your computer and use it in GitHub Desktop.
Save acorcutt/1934c85c852a0d2e8dff32066b0b0783 to your computer and use it in GitHub Desktop.
Next.js Page Transition
// Tailwinds Styles
import '../styles/globals.css';
import { useRouter } from 'next/router';
import { useState, useEffect } from 'react';
import classNames from 'classnames';
function MyApp({ Component, pageProps }) {
const [transition, setTransition] = useState('opacity-100');
// Router Events
useEffect(() => {
const router = useRouter();
const handleRouteChange = (url) => {
setTransition('opacity-0');
};
const handleRouteChangeComplete = (url) => {
setTransition('opacity-100');
};
router.events.on('routeChangeStart', handleRouteChange);
router.events.on('routeChangeComplete', handleRouteChangeComplete);
return () => {
router.events.off('routeChangeStart', handleRouteChange);
router.events.off('routeChangeComplete', handleRouteChangeComplete);
};
}, []);
return (
<div className={classNames('transition duration-300', transition)}>
<Component {...pageProps} />
</div>
);
}
export default MyApp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment