Skip to content

Commit

Permalink
fix with-loading example for next 11 (#26569)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
johnrackles authored Jun 24, 2021
1 parent 18a333e commit bbedbb7
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions examples/with-loading/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import Router from 'next/router'
import { useEffect } from 'react'
import Link from 'next/link'
import Head from 'next/head'
import { useRouter } from 'next/router'
import NProgress from 'nprogress'

Router.events.on('routeChangeStart', (url) => {
console.log(`Loading: ${url}`)
NProgress.start()
})
Router.events.on('routeChangeComplete', () => NProgress.done())
Router.events.on('routeChangeError', () => NProgress.done())

export default function App({ Component, pageProps }) {
const router = useRouter()

useEffect(() => {
const handleStart = (url) => {
console.log(`Loading: ${url}`)
NProgress.start()
}
const handleStop = () => {
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])

return (
<>
<Head>
Expand Down

0 comments on commit bbedbb7

Please sign in to comment.