Unverified Commit bbedbb72 authored by John's avatar John Committed by GitHub

fix with-loading example for next 11 (#26569)

## Documentation / Examples

- [X] Make sure the linting passes

This PR updates the with-loading example to follow the documentation of router events for next 11
parent 18a333e6
import Router from 'next/router' import { useEffect } from 'react'
import Link from 'next/link' import Link from 'next/link'
import Head from 'next/head' import Head from 'next/head'
import { useRouter } from 'next/router'
import NProgress from 'nprogress' import NProgress from 'nprogress'
Router.events.on('routeChangeStart', (url) => { export default function App({ Component, pageProps }) {
const router = useRouter()
useEffect(() => {
const handleStart = (url) => {
console.log(`Loading: ${url}`) console.log(`Loading: ${url}`)
NProgress.start() NProgress.start()
}) }
Router.events.on('routeChangeComplete', () => NProgress.done()) const handleStop = () => {
Router.events.on('routeChangeError', () => NProgress.done()) NProgress.done()
}
router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeComplete', handleStop)
router.events.on('routeChangeError', handleStop)
return () => {
router.events.off('routeChangeStart', handleStart)
router.events.off('routeChangeComplete', handleStop)
router.events.off('routeChangeError', handleStop)
}
}, [router])
export default function App({ Component, pageProps }) {
return ( return (
<> <>
<Head> <Head>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment