rsnext/examples/with-unstated/pages/_app.js
Joe Haddad 18a9c7e371
Improve linting rules to catch more errors (#9374)
* Update `packages/`

* Update examples

* Update tests

* Update bench

* Update top level files

* Fix build

* trigger
2019-11-10 19:24:53 -08:00

37 lines
990 B
JavaScript

import App from 'next/app'
import React from 'react'
import { Provider } from 'unstated'
import { counterStore } from '../containers/CounterContainer'
class MyApp extends App {
static async getInitialProps() {
// do your server state here
if (typeof window === 'undefined') {
// reset state for each request
counterStore.resetState()
// process state, in this case counter start with 999
counterStore.initState(999)
return { serverState: counterStore.state }
} else {
return {}
}
}
constructor(props) {
super(props)
// pass the state to client store
// serverState will be reset when client navigate with Link
if (typeof window !== 'undefined') {
counterStore.initState(props.serverState.count)
}
}
render() {
const { Component, pageProps } = this.props
return (
<Provider inject={[counterStore]}>
<Component {...pageProps} />
</Provider>
)
}
}
export default MyApp