mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
def2ce7eda
Add an example how to use `@quentin-sommer/react-useragent` with next. I've been asked this https://github.com/quentin-sommer/react-useragent/issues/10 and plan to link this example as reference! See readme for details
42 lines
955 B
JavaScript
42 lines
955 B
JavaScript
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)
|