Created
May 24, 2018 11:58
-
-
Save jorngeorg/74271f8ea566414a7023c0fb0de306d7 to your computer and use it in GitHub Desktop.
Initial load and store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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