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
7ec57c6c
Unverified
Commit
7ec57c6c
authored
May 26, 2020
by
TodorTotev
Committed by
GitHub
May 26, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor with rematch example (#13392)
parent
658e0f8d
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
140 additions
and
151 deletions
+140
-151
package.json
examples/with-rematch/package.json
+5
-5
_app.js
examples/with-rematch/pages/_app.js
+1
-2
github-users.js
examples/with-rematch/pages/github-users.js
+55
-54
index.js
examples/with-rematch/pages/index.js
+27
-34
counter-display.js
examples/with-rematch/shared/components/counter-display.js
+23
-30
header.js
examples/with-rematch/shared/components/header.js
+16
-23
counter.js
examples/with-rematch/shared/models/counter.js
+6
-3
github.js
examples/with-rematch/shared/models/github.js
+1
-0
utils.js
examples/with-rematch/shared/utils.js
+6
-0
No files found.
examples/with-rematch/package.json
View file @
7ec57c6c
...
...
@@ -7,12 +7,12 @@
"start"
:
"next start"
},
"dependencies"
:
{
"@rematch/core"
:
"
^1.1
.0"
,
"@rematch/core"
:
"
1.4
.0"
,
"next"
:
"latest"
,
"react"
:
"
^16.8.4
"
,
"react-dom"
:
"
^16.8.4
"
,
"react-redux"
:
"
^6.0.1
"
,
"redux"
:
"
^4.0.1
"
"react"
:
"
16.13.1
"
,
"react-dom"
:
"
16.13.1
"
,
"react-redux"
:
"
7.2.0
"
,
"redux"
:
"
4.0.5
"
},
"license"
:
"ISC"
}
examples/with-rematch/pages/_app.js
View file @
7ec57c6c
import
App
from
'next/app'
import
React
from
'react
'
import
{
Provider
}
from
'react-redux
'
import
withRematch
from
'../shared/withRematch'
import
{
Provider
}
from
'react-redux'
class
MyApp
extends
App
{
render
()
{
...
...
examples/with-rematch/pages/github-users.js
View file @
7ec57c6c
import
{
Component
}
from
'react'
import
Link
from
'next/link'
import
{
connect
}
from
'react-redux'
import
{
checkServer
}
from
'../shared/utils'
import
{
useSelector
}
from
'react-redux'
import
{
useRematchDispatch
}
from
'../shared/utils'
import
{
initializeStore
}
from
'../shared/store'
import
CounterDisplay
from
'../shared/components/counter-display'
import
Header
from
'../shared/components/header'
class
Github
extends
Component
{
static
async
getInitialProps
(
ctx
)
{
const
store
=
ctx
.
reduxStore
// Pre-populate users only on the server-side
if
(
checkServer
())
{
await
store
.
dispatch
.
github
.
fetchUsers
()
}
return
{}
}
const
Github
=
(
props
)
=>
{
const
github
=
useSelector
((
state
)
=>
state
.
github
)
const
{
users
,
isLoading
}
=
github
const
{
fetchUsers
}
=
useRematchDispatch
((
dispatch
)
=>
({
fetchUsers
:
dispatch
.
github
.
fetchUsers
,
}))
render
()
{
const
{
isLoading
,
fetchUsers
,
userList
}
=
this
.
props
const
{
usersList
}
=
props
return
(
<
div
>
<
Header
/>
<
h1
>
Github
users
<
/h1
>
<
p
>
Server
rendered
github
user
list
.
You
can
also
reload
the
users
from
the
api
by
clicking
the
<
b
>
Get
users
<
/b> button below
.
<
/p
>
<
h1
>
Users
passed
as
property
from
getStaticProps
<
/h1
>
{
usersList
.
map
((
user
)
=>
(
<
div
key
=
{
user
.
login
}
>
<
a
href
=
{
user
.
html_url
}
>
<
img
height
=
"45"
width
=
"45"
src
=
{
user
.
avatar_url
}
/
>
<
span
>
Username
-
{
user
.
login
}
<
/span
>
<
/a
>
<
br
/>
<
/div
>
))}
{
isLoading
?
<
p
>
Loading
...
<
/p> : null
}
<
p
>
<
button
onClick
=
{
fetchUsers
}
>
Get
users
<
/button
>
<
/p
>
{
users
.
length
===
0
?
null
:
<
h1
>
Users
fetched
from
async
function
<
/h1>
}
{
users
.
map
((
user
)
=>
(
<
div
key
=
{
user
.
login
}
>
<
a
href
=
{
user
.
html_url
}
>
<
img
height
=
"45"
width
=
"45"
src
=
{
user
.
avatar_url
}
/
>
<
span
>
Username
-
{
user
.
login
}
<
/span
>
<
/a
>
<
br
/>
<
/div
>
))}
<
br
/>
<
CounterDisplay
/>
<
/div
>
)
}
return
(
<
div
>
<
Header
/>
<
h1
>
Github
users
<
/h1
>
<
p
>
Server
rendered
github
user
list
.
You
can
also
reload
the
users
from
the
api
by
clicking
the
<
b
>
Get
users
<
/b> button below
.
<
/p
>
{
isLoading
?
<
p
>
Loading
...
<
/p> : null
}
<
p
>
<
button
onClick
=
{
fetchUsers
}
>
Get
users
<
/button
>
<
/p
>
{
userList
.
map
((
user
)
=>
(
<
div
key
=
{
user
.
login
}
>
<
Link
href
=
{
user
.
html_url
}
passHref
>
<
a
>
<
img
height
=
"45"
width
=
"45"
src
=
{
user
.
avatar_url
}
/
>
<
span
>
Username
-
{
user
.
login
}
<
/span
>
<
/a
>
<
/Link
>
<
br
/>
<
/div
>
))}
<
br
/>
export
async
function
getStaticProps
()
{
const
store
=
initializeStore
()
const
usersList
=
await
store
.
dispatch
.
github
.
fetchUsers
()
<
CounterDisplay
/>
<
/div
>
)
return
{
props
:
{
usersList
,
},
}
}
const
mapState
=
(
state
)
=>
({
userList
:
state
.
github
.
users
,
isLoading
:
state
.
github
.
isLoading
,
})
const
mapDispatch
=
({
github
:
{
fetchUsers
}
})
=>
({
fetchUsers
:
()
=>
fetchUsers
(),
})
export
default
connect
(
mapState
,
mapDispatch
)(
Github
)
export
default
Github
examples/with-rematch/pages/index.js
View file @
7ec57c6c
import
{
Component
}
from
'react'
import
{
connect
}
from
'react-redux'
import
{
useSelector
,
useDispatch
}
from
'react-redux'
import
Header
from
'../shared/components/header'
import
{
useRematchDispatch
}
from
'../shared/utils'
class
Home
extends
Component
{
render
()
{
const
{
counter
,
increment
,
incrementBy
,
incrementAsync
}
=
this
.
props
const
Home
=
()
=>
{
const
counter
=
useSelector
((
state
)
=>
state
.
counter
)
const
dispatch
=
useDispatch
()
const
{
increment
}
=
useRematchDispatch
((
dispatch
)
=>
({
increment
:
dispatch
.
counter
.
increment
,
}))
return
(
<
div
>
<
Header
/>
<
h1
>
Counter
<
/h1
>
<
h3
>
The
count
is
{
counter
}
<
/h3
>
<
p
>
<
button
onClick
=
{
increment
}
>
increment
<
/button
>
<
button
onClick
=
{()
=>
increment
(
1
)}
>
increment
(
using
dispatch
function
)
<
/button
>
<
button
onClick
=
{
incrementBy
(
5
)}
>
increment
by
5
<
/button
>
<
button
onClick
=
{
incrementAsync
}
>
incrementAsync
<
/button
>
<
/p
>
<
br
/>
<
/div
>
)
}
return
(
<
div
>
<
Header
/>
<
h1
>
Counter
<
/h1
>
<
h3
>
The
count
is
{
counter
}
<
/h3
>
<
p
>
<
button
onClick
=
{
dispatch
.
counter
.
increment
}
>
increment
<
/button
>
<
button
onClick
=
{()
=>
increment
(
1
)}
>
increment
(
using
dispatch
function
)
<
/button
>
<
button
onClick
=
{()
=>
increment
(
5
)}
>
increment
by
5
<
/button
>
<
button
onClick
=
{
dispatch
.
counter
.
incrementAsync
}
>
incrementAsync
<
/button
>
<
/p
>
<
br
/>
<
/div
>
)
}
const
mapState
=
(
state
)
=>
({
counter
:
state
.
counter
,
})
const
mapDispatch
=
({
counter
:
{
increment
,
incrementAsync
}
})
=>
({
increment
:
()
=>
increment
(
1
),
incrementBy
:
(
amount
)
=>
()
=>
increment
(
amount
),
incrementAsync
:
()
=>
incrementAsync
(
1
),
})
export
default
connect
(
mapState
,
mapDispatch
)(
Home
)
export
default
Home
examples/with-rematch/shared/components/counter-display.js
View file @
7ec57c6c
import
React
,
{
Component
}
from
'react'
import
{
connect
}
from
'react-redux'
import
React
from
'react'
import
{
useSelector
}
from
'react-redux'
import
{
useRematchDispatch
}
from
'../utils'
c
lass
CounterDisplay
extends
Component
{
render
()
{
const
{
counter
,
incrementBy3
}
=
this
.
props
return
(
<
div
>
<
h3
>
Counter
<
/h3
>
<
p
>
This
counter
is
connected
via
the
<
b
>
connect
<
/b> function. Component
s
which
are
not
pages
can
be
connected
using
the
connect
function
just
like
redux
components
.
<
/p
>
<
p
>
Current
value
{
counter
}
<
/p
>
<
p
>
<
button
onClick
=
{
incrementBy3
}
>
Increment
by
3
<
/button
>
<
/p
>
<
/
div
>
)
}
c
onst
CounterDisplay
=
()
=>
{
const
counter
=
useSelector
((
state
)
=>
state
.
counter
)
const
{
increment
}
=
useRematchDispatch
((
dispatch
)
=>
({
increment
:
dispatch
.
counter
.
increment
,
}))
return
(
<
>
<
h3
>
Counter
<
/h3
>
<
p
>
This
counter
is
connected
via
the
<
b
>
useSelector
<
/b> hook. Component
s
which
are
not
pages
can
be
connected
using
the
useSelector
hook
just
like
redux
components
.
<
/p
>
<
p
>
Current
value
{
counter
}
<
/
p
>
<
p
>
<
button
onClick
=
{()
=>
increment
(
3
)}
>
Increment
by
3
<
/button
>
<
/
p
>
<
/
>
)
}
const
mapState
=
(
state
)
=>
({
counter
:
state
.
counter
,
})
const
mapDispatch
=
({
counter
:
{
increment
,
incrementAsync
}
})
=>
({
incrementBy3
:
()
=>
increment
(
3
),
})
export
default
connect
(
mapState
,
mapDispatch
)(
CounterDisplay
)
export
default
CounterDisplay
examples/with-rematch/shared/components/header.js
View file @
7ec57c6c
import
React
,
{
Component
}
from
'react'
import
Link
from
'next/link'
import
React
from
'react'
class
Header
extends
Component
{
render
()
{
return
(
<
div
>
<
nav
>
<
ul
>
<
li
>
<
Link
href
=
"/"
passHref
>
<
a
>
Home
<
/a
>
<
/Link
>
<
/li
>
<
li
>
<
Link
href
=
"/github-users"
passHref
>
<
a
>
Async
Example
<
/a
>
<
/Link
>
<
/li
>
<
/ul
>
<
/nav
>
<
/div
>
)
}
const
Header
=
()
=>
{
return
(
<
div
>
<
nav
>
<
ul
>
<
li
>
<
a
href
=
"/"
>
Home
<
/a
>
<
/li
>
<
li
>
<
a
href
=
"/github-users"
>
Async
Example
<
/a
>
<
/li
>
<
/ul
>
<
/nav
>
<
/div
>
)
}
export
default
Header
examples/with-rematch/shared/models/counter.js
View file @
7ec57c6c
const
counter
=
{
import
{
createModel
}
from
'@rematch/core'
const
counter
=
createModel
({
state
:
0
,
// initial state
reducers
:
{
// handle state changes with pure functions
increment
(
state
,
payload
)
{
increment
:
(
state
,
payload
)
=>
{
if
(
typeof
payload
===
'object'
)
return
state
+
1
return
state
+
payload
},
},
...
...
@@ -14,6 +17,6 @@ const counter = {
this
.
increment
(
payload
)
},
},
}
}
)
export
default
counter
examples/with-rematch/shared/models/github.js
View file @
7ec57c6c
...
...
@@ -26,6 +26,7 @@ const github = {
const
response
=
await
fetch
(
'https://api.github.com/users'
)
const
users
=
await
response
.
json
()
this
.
receiveUsers
(
users
)
return
users
}
catch
(
err
)
{
console
.
log
(
err
)
this
.
receiveUsers
([])
...
...
examples/with-rematch/shared/utils.js
View file @
7ec57c6c
// Robust way to check if it's Node or browser
import
{
useDispatch
}
from
'react-redux'
export
const
checkServer
=
()
=>
{
return
typeof
window
===
'undefined'
}
export
const
useRematchDispatch
=
(
selector
)
=>
{
const
dispatch
=
useDispatch
()
return
selector
(
dispatch
)
}
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