提交 08b14e49 编写于 作者: J Junyoung Choi 提交者: Luis Fernando Alvarez D

Simplify mobx example (#8269)

上级 43370c1f
import App, { Container } from 'next/app' import App, { Container } from 'next/app'
import React from 'react' import React from 'react'
import { initializeStore } from '../store' import { fetchInitialStoreState, Store } from '../store'
import { Provider } from 'mobx-react' import { Provider } from 'mobx-react'
class MyMobxApp extends App { class MyMobxApp extends App {
static async getInitialProps(appContext) { state = {
// Get or Create the store with `undefined` as initialState store: new Store(),
// This allows you to set a custom default initialState }
const mobxStore = initializeStore()
// Provide the store to getInitialProps of pages
appContext.ctx.mobxStore = mobxStore
let appProps = await App.getInitialProps(appContext) // Fetching serialized(JSON) store state
static async getInitialProps(appContext) {
const appProps = await App.getInitialProps(appContext)
const initialStoreState = await fetchInitialStoreState()
return { return {
...appProps, ...appProps,
initialMobxState: mobxStore, initialStoreState,
} }
} }
constructor(props) { // Hydrate serialized state to store
super(props) static getDerivedStateFromProps(props, state) {
const isServer = typeof window === 'undefined' state.store.hydrate(props.initialStoreState)
this.mobxStore = isServer return state
? props.initialMobxState
: initializeStore(props.initialMobxState)
} }
render() { render() {
const { Component, pageProps } = this.props const { Component, pageProps } = this.props
return ( return (
<Container> <Container>
<Provider store={this.mobxStore}> <Provider store={this.state.store}>
<Component {...pageProps} /> <Component {...pageProps} />
</Provider> </Provider>
</Container> </Container>
......
...@@ -4,14 +4,16 @@ import { useStaticRendering } from 'mobx-react' ...@@ -4,14 +4,16 @@ import { useStaticRendering } from 'mobx-react'
const isServer = typeof window === 'undefined' const isServer = typeof window === 'undefined'
useStaticRendering(isServer) useStaticRendering(isServer)
class Store { export class Store {
@observable lastUpdate = 0 @observable lastUpdate = 0
@observable light = false @observable light = false
constructor(isServer, initialData = {}) { hydrate(serializedStore) {
this.lastUpdate = this.lastUpdate =
initialData.lastUpdate != null ? initialData.lastUpdate : Date.now() serializedStore.lastUpdate != null
this.light = !!initialData.light ? serializedStore.lastUpdate
: Date.now()
this.light = !!serializedStore.light
} }
@action start = () => { @action start = () => {
...@@ -24,15 +26,7 @@ class Store { ...@@ -24,15 +26,7 @@ class Store {
stop = () => clearInterval(this.timer) stop = () => clearInterval(this.timer)
} }
let store = null export async function fetchInitialStoreState() {
// You can do anything to fetch initial store state
export function initializeStore(initialData) { return {}
// Always make a new store if server, otherwise state is shared between requests
if (isServer) {
return new Store(isServer, initialData)
}
if (store === null) {
store = new Store(isServer, initialData)
}
return store
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册