Loading Screen on next js page transition

前端 未结 3 2024
礼貌的吻别
礼貌的吻别 2021-02-04 13:53

I am trying to implement a loading screen when changing routes in my Nextjs app ,for example /home -> /about.

My current implementation is as follows. I am setting the

3条回答
  •  感情败类
    2021-02-04 14:18

    Why not use nprogress as follows in _app.js

    import React from 'react';
    import Router from 'next/router';
    import App, { Container } from 'next/app';
    import NProgress from 'nprogress';
    
    NProgress.configure({ showSpinner: publicRuntimeConfig.NProgressShowSpinner });
    
    Router.onRouteChangeStart = () => {
      // console.log('onRouteChangeStart triggered');
      NProgress.start();
    };
    
    Router.onRouteChangeComplete = () => {
      // console.log('onRouteChangeComplete triggered');
      NProgress.done();
    };
    
    Router.onRouteChangeError = () => {
      // console.log('onRouteChangeError triggered');
      NProgress.done();
    };
    
    export default class MyApp extends App { ... }
    

    Link to nprogress.

    You also need to include style file as well. If you put the css file in static directory, then you can access the style as follows:

    
    

    Make sure the CSS is available in all pages...

    It will work for all your routes changing.

提交回复
热议问题