1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-react-useragent/pages/_app.js

42 lines
955 B
JavaScript
Raw Normal View History

import React from 'react'
import App, {Container} from 'next/app'
import {UserAgentProvider} from '@quentin-sommer/react-useragent'
const PageWrapper = Comp =>
class extends React.Component {
/*
* Need to use args.ctx
* 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>
<Component {...pageProps} />
</Container>
)
}
}
export default PageWrapper(MyApp)