Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
08b14e49
Commit
08b14e49
authored
Aug 08, 2019
by
Junyoung Choi
Committed by
Luis Fernando Alvarez D
Aug 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify mobx example (#8269)
parent
43370c1f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
31 deletions
+23
-31
_app.js
examples/with-mobx/pages/_app.js
+14
-16
store.js
examples/with-mobx/store.js
+9
-15
No files found.
examples/with-mobx/pages/_app.js
View file @
08b14e49
import
App
,
{
Container
}
from
'next/app'
import
React
from
'react'
import
{
initialize
Store
}
from
'../store'
import
{
fetchInitialStoreState
,
Store
}
from
'../store'
import
{
Provider
}
from
'mobx-react'
class
MyMobxApp
extends
App
{
static
async
getInitialProps
(
appContext
)
{
// Get or Create the store with `undefined` as initialState
// This allows you to set a custom default initialState
const
mobxStore
=
initializeStore
()
// Provide the store to getInitialProps of pages
appContext
.
ctx
.
mobxStore
=
mobxStore
state
=
{
store
:
new
Store
(),
}
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
{
...
appProps
,
initial
MobxState
:
mobxStor
e
,
initial
StoreStat
e
,
}
}
constructor
(
props
)
{
super
(
props
)
const
isServer
=
typeof
window
===
'undefined'
this
.
mobxStore
=
isServer
?
props
.
initialMobxState
:
initializeStore
(
props
.
initialMobxState
)
// Hydrate serialized state to store
static
getDerivedStateFromProps
(
props
,
state
)
{
state
.
store
.
hydrate
(
props
.
initialStoreState
)
return
state
}
render
()
{
const
{
Component
,
pageProps
}
=
this
.
props
return
(
<
Container
>
<
Provider
store
=
{
this
.
mobxS
tore
}
>
<
Provider
store
=
{
this
.
state
.
s
tore
}
>
<
Component
{...
pageProps
}
/
>
<
/Provider
>
<
/Container
>
...
...
examples/with-mobx/store.js
View file @
08b14e49
...
...
@@ -4,14 +4,16 @@ import { useStaticRendering } from 'mobx-react'
const
isServer
=
typeof
window
===
'undefined'
useStaticRendering
(
isServer
)
class
Store
{
export
class
Store
{
@
observable
lastUpdate
=
0
@
observable
light
=
false
constructor
(
isServer
,
initialData
=
{}
)
{
hydrate
(
serializedStore
)
{
this
.
lastUpdate
=
initialData
.
lastUpdate
!=
null
?
initialData
.
lastUpdate
:
Date
.
now
()
this
.
light
=
!!
initialData
.
light
serializedStore
.
lastUpdate
!=
null
?
serializedStore
.
lastUpdate
:
Date
.
now
()
this
.
light
=
!!
serializedStore
.
light
}
@
action
start
=
()
=>
{
...
...
@@ -24,15 +26,7 @@ class Store {
stop
=
()
=>
clearInterval
(
this
.
timer
)
}
let
store
=
null
export
function
initializeStore
(
initialData
)
{
// 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
export
async
function
fetchInitialStoreState
()
{
// You can do anything to fetch initial store state
return
{}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment