Last active
June 23, 2021 12:04
-
-
Save acorcutt/1934c85c852a0d2e8dff32066b0b0783 to your computer and use it in GitHub Desktop.
Next.js Page Transition
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
// 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