Created
September 30, 2019 15:49
-
-
Save SvSven/d7e0e77f63e95065cd30c4cf9ef012e4 to your computer and use it in GitHub Desktop.
Show/hide a loading screen, message or animation on client side routing in Next.js
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 { useState, useEffect } from "react"; | |
import Router from "next/router"; | |
const Layout = props => { | |
const [isLoading, setIsLoading] = useState(false); | |
useEffect(() => { | |
const updateLoadingStatus = () => setIsLoading(!isLoading); | |
Router.events.on("routeChangeStart", updateLoadingStatus); | |
Router.events.on("routeChangeComplete", updateLoadingStatus); | |
return () => { | |
Router.events.off("routeChangeStart", updateLoadingStatus); | |
Router.events.off("routeChangeComplete", updateLoadingStatus); | |
}; | |
}, [isLoading]); | |
return ( | |
<div> | |
{ isLoading ? <p>Loading...</p> : props.children } | |
</div> | |
); | |
} | |
export default Layout; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment