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
a25359bf
Commit
a25359bf
authored
Jun 30, 2018
by
Junyoung Choi (Sai)
Committed by
Tim Neutkens
Jun 30, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve with-mobx example (#4705)
in flavor of
https://github.com/zeit/next.js/pull/4377
parent
de6bf4e7
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
33 deletions
+72
-33
with-mobx-store.js
examples/with-mobx/lib/with-mobx-store.js
+50
-0
_app.js
examples/with-mobx/pages/_app.js
+19
-0
index.js
examples/with-mobx/pages/index.js
+1
-16
other.js
examples/with-mobx/pages/other.js
+1
-16
store.js
examples/with-mobx/store.js
+1
-1
No files found.
examples/with-mobx/lib/with-mobx-store.js
0 → 100644
View file @
a25359bf
import
React
from
'react'
import
{
initializeStore
}
from
'../store'
const
isServer
=
typeof
window
===
'undefined'
const
__NEXT_MOBX_STORE__
=
'__NEXT_MOBX_STORE__'
function
getOrCreateStore
(
initialState
)
{
// Always make a new store if server, otherwise state is shared between requests
if
(
isServer
)
{
return
initializeStore
(
initialState
)
}
// Create store if unavailable on the client and set it on the window object
if
(
!
window
[
__NEXT_MOBX_STORE__
])
{
window
[
__NEXT_MOBX_STORE__
]
=
initializeStore
(
initialState
)
}
return
window
[
__NEXT_MOBX_STORE__
]
}
export
default
(
App
)
=>
{
return
class
AppWithMobx
extends
React
.
Component
{
static
async
getInitialProps
(
appContext
)
{
// Get or Create the store with `undefined` as initialState
// This allows you to set a custom default initialState
const
mobxStore
=
getOrCreateStore
()
// Provide the store to getInitialProps of pages
appContext
.
ctx
.
mobxStore
=
mobxStore
let
appProps
=
{}
if
(
typeof
App
.
getInitialProps
===
'function'
)
{
appProps
=
await
App
.
getInitialProps
.
call
(
App
,
appContext
)
}
return
{
...
appProps
,
initialMobxState
:
mobxStore
}
}
constructor
(
props
)
{
super
(
props
)
this
.
mobxStore
=
getOrCreateStore
(
props
.
initialMobxState
)
}
render
()
{
return
<
App
{...
this
.
props
}
mobxStore
=
{
this
.
mobxStore
}
/
>
}
}
}
examples/with-mobx/pages/_app.js
0 → 100644
View file @
a25359bf
import
App
,
{
Container
}
from
'next/app'
import
React
from
'react'
import
withMobxStore
from
'../lib/with-mobx-store'
import
{
Provider
}
from
'mobx-react'
class
MyApp
extends
App
{
render
()
{
const
{
Component
,
pageProps
,
mobxStore
}
=
this
.
props
return
(
<
Container
>
<
Provider
store
=
{
mobxStore
}
>
<
Component
{...
pageProps
}
/
>
<
/Provider
>
<
/Container
>
)
}
}
export
default
withMobxStore
(
MyApp
)
examples/with-mobx/pages/index.js
View file @
a25359bf
import
React
from
'react'
import
{
Provider
}
from
'mobx-react'
import
{
initStore
}
from
'../store'
import
Page
from
'../components/Page'
export
default
class
Counter
extends
React
.
Component
{
static
getInitialProps
({
req
})
{
const
isServer
=
!!
req
const
store
=
initStore
(
isServer
)
return
{
lastUpdate
:
store
.
lastUpdate
,
isServer
}
}
constructor
(
props
)
{
super
(
props
)
this
.
store
=
initStore
(
props
.
isServer
,
props
.
lastUpdate
)
}
render
()
{
return
(
<
Provider
store
=
{
this
.
store
}
>
<
Page
title
=
'Index Page'
linkTo
=
'/other'
/>
<
/Provider
>
<
Page
title
=
'Index Page'
linkTo
=
'/other'
/>
)
}
}
examples/with-mobx/pages/other.js
View file @
a25359bf
import
React
from
'react'
import
{
Provider
}
from
'mobx-react'
import
{
initStore
}
from
'../store'
import
Page
from
'../components/Page'
export
default
class
Counter
extends
React
.
Component
{
static
getInitialProps
({
req
})
{
const
isServer
=
!!
req
const
store
=
initStore
(
isServer
)
return
{
lastUpdate
:
store
.
lastUpdate
,
isServer
}
}
constructor
(
props
)
{
super
(
props
)
this
.
store
=
initStore
(
props
.
isServer
,
props
.
lastUpdate
)
}
render
()
{
return
(
<
Provider
store
=
{
this
.
store
}
>
<
Page
title
=
'Other Page'
linkTo
=
'/'
/>
<
/Provider
>
<
Page
title
=
'Other Page'
linkTo
=
'/'
/>
)
}
}
examples/with-mobx/store.js
View file @
a25359bf
...
...
@@ -20,7 +20,7 @@ class Store {
stop
=
()
=>
clearInterval
(
this
.
timer
)
}
export
function
initStore
(
isServer
,
lastUpdate
=
Date
.
now
())
{
export
function
init
ialize
Store
(
isServer
,
lastUpdate
=
Date
.
now
())
{
if
(
isServer
)
{
return
new
Store
(
isServer
,
lastUpdate
)
}
else
{
...
...
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