Unverified Commit e36d7b32 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Add example route event code (#7745)

parent f8097e51
...@@ -782,7 +782,7 @@ Here's a list of supported events: ...@@ -782,7 +782,7 @@ Here's a list of supported events:
- `routeChangeStart(url)` - Fires when a route starts to change - `routeChangeStart(url)` - Fires when a route starts to change
- `routeChangeComplete(url)` - Fires when a route changed completely - `routeChangeComplete(url)` - Fires when a route changed completely
- `routeChangeError(err, url)` - Fires when there's an error when changing routes - `routeChangeError(err, url)` - Fires when there's an error when changing routes, or a route load is cancelled
- `beforeHistoryChange(url)` - Fires just before changing the browser's history - `beforeHistoryChange(url)` - Fires just before changing the browser's history
- `hashChangeStart(url)` - Fires when the hash will change but not the page - `hashChangeStart(url)` - Fires when the hash will change but not the page
- `hashChangeComplete(url)` - Fires when the hash has changed but not the page - `hashChangeComplete(url)` - Fires when the hash has changed but not the page
...@@ -817,6 +817,18 @@ Router.events.on('routeChangeError', (err, url) => { ...@@ -817,6 +817,18 @@ Router.events.on('routeChangeError', (err, url) => {
> **Note**: Using router events in `getInitialProps` is discouraged as it may result in unexpected behavior.<br/> > **Note**: Using router events in `getInitialProps` is discouraged as it may result in unexpected behavior.<br/>
> Router events should be registered when a component mounts (`useEffect` or `componentDidMount`/`componentWillUnmount`) or imperatively when an event happens. > Router events should be registered when a component mounts (`useEffect` or `componentDidMount`/`componentWillUnmount`) or imperatively when an event happens.
> ```js
> useEffect(() => {
> const handleRouteChange = url => {
> console.log('App is changing to: ', url)
> }
>
> Router.events.on('routeChangeStart', handleRouteChange)
> return () => {
> Router.events.off('routeChangeStart', handleRouteChange)
> }
> }, [])
> ```
##### Shallow Routing ##### Shallow Routing
......
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