Skip to content

Instantly share code, notes, and snippets.

@quentin-sommer
Last active October 10, 2018 08:03
Show Gist options
  • Save quentin-sommer/b4e14e69412db4b4356a860b1584569a to your computer and use it in GitHub Desktop.
Save quentin-sommer/b4e14e69412db4b4356a860b1584569a to your computer and use it in GitHub Desktop.
/* imports */
const PageWrapper = Comp =>
class extends React.Component {
/*
* We need to use args.ctx here instead of args
* See https://github.com/zeit/next.js#custom-document
*/
static async getInitialProps(args) {
return {
ua: args.ctx.req
? args.ctx.req.headers['user-agent']
: navigator.userAgent,
...(Comp.getInitialProps ? await Comp.getInitialProps(args) : null),
}
}
render() {
const {ua, ...props} = this.props
return (
<UserAgentProvider ua={ua}>
<Comp {...props} />
</UserAgentProvider>
)
}
}
class MyApp extends App {
render() {
const {Component, pageProps} = this.props
return (
<Container>
<Page>
<Component {...pageProps} />
</Page>
</Container>
)
}
}
// use the PageWrapper only once!
export default PageWrapper(MyApp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment