Skip to content

Instantly share code, notes, and snippets.

@SvSven
Created September 30, 2019 15:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SvSven/d7e0e77f63e95065cd30c4cf9ef012e4 to your computer and use it in GitHub Desktop.
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
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