Created
July 10, 2018 13:35
-
-
Save HaNdTriX/62e7f02d1b8b425d09469ad42afdb450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Router from 'next/router' | |
const HistoryContext = React.createContext([]); | |
export class HistoryProvider extends React.Component { | |
state = { | |
history: [] | |
} | |
componentDidMount() { | |
this.handleRouteChangeComplete(Router.route) | |
Router.events.on('routeChangeComplete', this.handleRouteChangeComplete) | |
} | |
componentWillUnmount() { | |
Router.events.off('routeChangeComplete', this.handleRouteChangeComplete) | |
} | |
handleRouteChangeComplete = (url) => { | |
this.setState(state => ({ | |
history: [ | |
...state.history, | |
url | |
] | |
})) | |
} | |
render() { | |
return ( | |
<HistoryContext.Provider value={this.state.history}> | |
{this.props.children} | |
</HistoryContext.Provider> | |
) | |
} | |
} | |
export const HistoryConsumer = HistoryContext.Consumer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
pages/_app.js
components/History.js