Skip to content

Commit

Permalink
improve with-unstated example (#5998)
Browse files Browse the repository at this point in the history
improve the example so that it can preserve unstated from server to client unstated
  • Loading branch information
tylim88 authored and timneutkens committed Jan 6, 2019
1 parent ba95f75 commit 5d8ae44
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/with-carlo/pages/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import Link from 'next/link'

export default class About extends React.Component {
state = { name: '' };
state = { name: '' }

async componentDidMount () {
const name = await window.getName()
Expand Down
3 changes: 1 addition & 2 deletions examples/with-unstated/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ now

This example shows how to integrate Unstated in Next.js. For more info about Unstated you can visit [here](https://github.com/jamiebuilds/unstated). The example is basically same as [redux example](https://github.com/zeit/next.js/tree/canary/examples/with-redux).

This example also shows how to share state within different page, you can expect the same state for Counter when switching between Index and About.

The "counter" shows you how to preserve state from server to client and share the state within different page, you can expect the same state for "counter" when switching between Index and About page, observe that "counter" behaves differently from the "clock" example.
9 changes: 9 additions & 0 deletions examples/with-unstated/containers/CounterContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ export default class CounterContainer extends Container {
reset = () => {
this.setState({ count: 0 })
}

// this two methods are not setState as they work only before rendering
initState = count => {
this.state = { count }
}
resetState = () => {
this.initState(0)
}
}
export const counterStore = new CounterContainer()
26 changes: 22 additions & 4 deletions examples/with-unstated/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import App, { Container } from 'next/app'
import React from 'react'
import { Provider } from 'unstated'
import { CounterContainer } from '../containers'

const counter = new CounterContainer()
import { counterStore } from '../containers/CounterContainer'

class MyApp extends App {
static async getInitialProps () {
// do your server state here
if (!process.browser) {
// 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 (process.browser) {
counterStore.initState(props.serverState.count)
}
}
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Provider inject={[counter]}>
<Provider inject={[counterStore]}>
<Component {...pageProps} />
</Provider>
</Container>
Expand Down

0 comments on commit 5d8ae44

Please sign in to comment.