Is it possible to use a <Loading />
component in NextJS instead of nprogress? I'm thinking you would need to access some high level props like pageProps
from the router events so you can toggle the loading state and then conditionally output a loading component but I don't see a way to do this...
For example,
Router.events.on("routeChangeStart", (url, pageProps) => {
pageProps.loading = true;
});
and in the render
const { Component, pageProps } = this.props;
return pageProps.loading ? <Loading /> : <Page />;
But of course, routeChangeStart
doesn't pass in pageProps.
Yes, it is possible to use another component to handle loading instead of nProgress. I had a similar question, and found the solution below working out for me.
It makes sense to do all this in ./pages/_app.js
, because according to the documentation that can help with persisting layout and state between pages. You can initiate the Router events on the lifecycle method componentDidMount
. It's important to note that _app.js
only mounts once, but initiating Router events will still work here. This will allow you to be able to set state.
Below is an example of how this all comes together:
import React, { Fragment } from 'react';
import App from 'next/app';
import Head from 'next/head';
import Router from 'next/router';
class MyApp extends App {
state = { isLoading: false }
componentDidMount() {
// Logging to prove _app.js only mounts once,
// but initializing router events here will also accomplishes
// goal of setting state on route change
console.log('MOUNT');
Router.events.on('routeChangeStart', () => {
this.setState({ isLoading: true });
});
Router.events.on('routeChangeComplete', () => {
this.setState({ isLoading: false });
});
Router.events.on('routeChangeError', () => {
this.setState({ isLoading: false });
});
}
render() {
const { Component, pageProps } = this.props;
const { isLoading } = this.state;
return (
<Fragment>
<Head>
<title>My App</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charSet="utf-8" />
</Head>
{/* You could also pass isLoading state to Component and handle logic there */}
<Component {...pageProps} />
{isLoading && 'STRING OR LOADING COMPONENT HERE...'}
</Fragment>
);
}
}
MyApp.getInitialProps = async ({ Component, ctx }) => {
let pageProps = {};
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx);
}
return { pageProps };
};
export default MyApp;
Use the hook below to inspect router's loading status:
import Router from 'next/router'
import { useState, useEffect } from 'react'
const useRouterLoading = () => {
const [loading, setLoading] = useState(false)
useEffect(() => {
const start = () => setLoading(true)
const end = () => setLoading(false)
Router.events.on('routeChangeStart', start)
Router.events.on('routeChangeComplete', end)
Router.events.on('routeChangeError', end)
return () => {
Router.events.off('routeChangeStart', start)
Router.events.off('routeChangeComplete', end)
Router.events.off('routeChangeError', end)
}
}, [])
return loading
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With