Skip to content

Instantly share code, notes, and snippets.

@jorngeorg
Created May 24, 2018 11:58
Show Gist options
  • Save jorngeorg/74271f8ea566414a7023c0fb0de306d7 to your computer and use it in GitHub Desktop.
Save jorngeorg/74271f8ea566414a7023c0fb0de306d7 to your computer and use it in GitHub Desktop.
Initial load and store
import App, {Container} from 'next/app'
import React from 'react'
const isServer = typeof window === 'undefined'
const clientStore = isServer ? null : {}
// mock of your actual data fetching solution
async function someFetch() {
return {}
}
export default class MyApp extends App {
static async getInitialProps ({ Component, router, ctx }) {
let pageProps = {}
let appData = clientStore ? clientStore.appData : await someFetch()
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return {appData, pageProps}
}
constructor(props) {
super(props)
if(clientStore && !clientStore.appData) {
clientStore.appData = props.appData
}
}
render () {
const {Component, pageProps} = this.props
return <Container>
<Component {...pageProps} />
</Container>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment