_app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import App, { Container } from 'next/app'
  2. import Page from '../components/Page'
  3. import { ApolloProvider } from 'react-apollo'
  4. import withApollo from '../lib/withApollo'
  5. /**
  6. * Next.js uses the `App` component to initialize pages. See:
  7. * https://github.com/zeit/next.js/#custom-app
  8. *
  9. * Example how to use it to style child components:
  10. * https://github.com/zeit/next.js/blob/canary/examples/with-app-layout/pages/_app.js
  11. *
  12. * Instead of the Layout component, we use the Page component here,
  13. * where we add a styled-components theme provider, and Next.js headers and metas.
  14. * - Using Page with layout information
  15. *
  16. * Using next-with-apollo:
  17. * https://github.com/lfades/next-with-apollo
  18. * - Wrapping MyApp in withApollo HOC
  19. */
  20. class MyApp extends App {
  21. static async getInitialProps ({ Component, ctx }) {
  22. let pageProps = {}
  23. if (Component.getInitialProps) {
  24. pageProps = await Component.getInitialProps(ctx)
  25. }
  26. // Add the query object to the pageProps
  27. // https://github.com/wesbos/Advanced-React/blob/master/finished-application/frontend/pages/_app.js
  28. pageProps.query = ctx.query
  29. return { pageProps }
  30. }
  31. render () {
  32. const { Component, apollo, pageProps } = this.props
  33. return (
  34. <Container>
  35. <ApolloProvider client={apollo}>
  36. <Page>
  37. <Component {...pageProps} />
  38. </Page>
  39. </ApolloProvider>
  40. </Container >
  41. )
  42. }
  43. }
  44. export default withApollo(MyApp)