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 @@
...
@@ -7,12 +7,12 @@
"start"
:
"next start"
"start"
:
"next start"
},
},
"dependencies"
:
{
"dependencies"
:
{
"@rematch/core"
:
"
^1.1
.0"
,
"@rematch/core"
:
"
1.4
.0"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"react"
:
"
^16.8.4
"
,
"react"
:
"
16.13.1
"
,
"react-dom"
:
"
^16.8.4
"
,
"react-dom"
:
"
16.13.1
"
,
"react-redux"
:
"
^6.0.1
"
,
"react-redux"
:
"
7.2.0
"
,
"redux"
:
"
^4.0.1
"
"redux"
:
"
4.0.5
"
},
},
"license"
:
"ISC"
"license"
:
"ISC"
}
}
examples/with-rematch/pages/_app.js
View file @
7ec57c6c
import
App
from
'next/app'
import
App
from
'next/app'
import
React
from
'react
'
import
{
Provider
}
from
'react-redux
'
import
withRematch
from
'../shared/withRematch'
import
withRematch
from
'../shared/withRematch'
import
{
Provider
}
from
'react-redux'
class
MyApp
extends
App
{
class
MyApp
extends
App
{
render
()
{
render
()
{
...
...
examples/with-rematch/pages/github-users.js
View file @
7ec57c6c
import
{
Component
}
from
'react'
import
{
useSelector
}
from
'react-redux'
import
Link
from
'next/link'
import
{
useRematchDispatch
}
from
'../shared/utils'
import
{
connect
}
from
'react-redux'
import
{
initializeStore
}
from
'../shared/store'
import
{
checkServer
}
from
'../shared/utils'
import
CounterDisplay
from
'../shared/components/counter-display'
import
CounterDisplay
from
'../shared/components/counter-display'
import
Header
from
'../shared/components/header'
import
Header
from
'../shared/components/header'
class
Github
extends
Component
{
const
Github
=
(
props
)
=>
{
static
async
getInitialProps
(
ctx
)
{
const
github
=
useSelector
((
state
)
=>
state
.
github
)
const
store
=
ctx
.
reduxStore
const
{
users
,
isLoading
}
=
github
const
{
fetchUsers
}
=
useRematchDispatch
((
dispatch
)
=>
({
// Pre-populate users only on the server-side
fetchUsers
:
dispatch
.
github
.
fetchUsers
,
if
(
checkServer
())
{
}))
await
store
.
dispatch
.
github
.
fetchUsers
()
}
return
{}
}
render
()
{
const
{
usersList
}
=
props
const
{
isLoading
,
fetchUsers
,
userList
}
=
this
.
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
(
export
async
function
getStaticProps
()
{
<
div
>
const
store
=
initializeStore
()
<
Header
/>
const
usersList
=
await
store
.
dispatch
.
github
.
fetchUsers
()
<
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
/>
<
CounterDisplay
/>
return
{
<
/div
>
props
:
{
)
usersList
,
},
}
}
}
}
const
mapState
=
(
state
)
=>
({
export
default
Github
userList
:
state
.
github
.
users
,
isLoading
:
state
.
github
.
isLoading
,
})
const
mapDispatch
=
({
github
:
{
fetchUsers
}
})
=>
({
fetchUsers
:
()
=>
fetchUsers
(),
})
export
default
connect
(
mapState
,
mapDispatch
)(
Github
)
examples/with-rematch/pages/index.js
View file @
7ec57c6c
import
{
Component
}
from
'react'
import
{
useSelector
,
useDispatch
}
from
'react-redux'
import
{
connect
}
from
'react-redux'
import
Header
from
'../shared/components/header'
import
Header
from
'../shared/components/header'
import
{
useRematchDispatch
}
from
'../shared/utils'
class
Home
extends
Component
{
const
Home
=
()
=>
{
render
()
{
const
counter
=
useSelector
((
state
)
=>
state
.
counter
)
const
{
counter
,
increment
,
incrementBy
,
incrementAsync
}
=
this
.
props
const
dispatch
=
useDispatch
()
const
{
increment
}
=
useRematchDispatch
((
dispatch
)
=>
({
increment
:
dispatch
.
counter
.
increment
,
}))
return
(
return
(
<
div
>
<
div
>
<
Header
/>
<
Header
/>
<
h1
>
Counter
<
/h1
>
<
h1
>
Counter
<
/h1
>
<
h3
>
The
count
is
{
counter
}
<
/h3
>
<
h3
>
The
count
is
{
counter
}
<
/h3
>
<
p
>
<
p
>
<
button
onClick
=
{
increment
}
>
increment
<
/button
>
<
button
onClick
=
{
dispatch
.
counter
.
increment
}
>
increment
<
/button
>
<
button
onClick
=
{()
=>
increment
(
1
)}
>
<
button
onClick
=
{()
=>
increment
(
1
)}
>
increment
(
using
dispatch
function
)
increment
(
using
dispatch
function
)
<
/button
>
<
/button
>
<
button
onClick
=
{
incrementBy
(
5
)}
>
increment
by
5
<
/button
>
<
button
onClick
=
{()
=>
increment
(
5
)}
>
increment
by
5
<
/button
>
<
button
onClick
=
{
incrementAsync
}
>
incrementAsync
<
/button
>
<
button
onClick
=
{
dispatch
.
counter
.
incrementAsync
}
>
<
/p
>
incrementAsync
<
br
/>
<
/button
>
<
/div
>
<
/p
>
)
<
br
/>
}
<
/div
>
)
}
}
const
mapState
=
(
state
)
=>
({
export
default
Home
counter
:
state
.
counter
,
})
const
mapDispatch
=
({
counter
:
{
increment
,
incrementAsync
}
})
=>
({
increment
:
()
=>
increment
(
1
),
incrementBy
:
(
amount
)
=>
()
=>
increment
(
amount
),
incrementAsync
:
()
=>
incrementAsync
(
1
),
})
export
default
connect
(
mapState
,
mapDispatch
)(
Home
)
examples/with-rematch/shared/components/counter-display.js
View file @
7ec57c6c
import
React
,
{
Component
}
from
'react'
import
React
from
'react'
import
{
connect
}
from
'react-redux'
import
{
useSelector
}
from
'react-redux'
import
{
useRematchDispatch
}
from
'../utils'
c
lass
CounterDisplay
extends
Component
{
c
onst
CounterDisplay
=
()
=>
{
render
()
{
const
counter
=
useSelector
((
state
)
=>
state
.
counter
)
const
{
counter
,
incrementBy3
}
=
this
.
props
const
{
increment
}
=
useRematchDispatch
((
dispatch
)
=>
({
increment
:
dispatch
.
counter
.
increment
,
return
(
}))
<
div
>
return
(
<
h3
>
Counter
<
/h3
>
<
>
<
p
>
<
h3
>
Counter
<
/h3
>
This
counter
is
connected
via
the
<
b
>
connect
<
/b> function. Component
s
<
p
>
which
are
not
pages
can
be
connected
using
the
connect
function
just
This
counter
is
connected
via
the
<
b
>
useSelector
<
/b> hook. Component
s
like
redux
components
.
which
are
not
pages
can
be
connected
using
the
useSelector
hook
just
<
/p
>
like
redux
components
.
<
p
>
Current
value
{
counter
}
<
/p
>
<
/p
>
<
p
>
<
p
>
Current
value
{
counter
}
<
/
p
>
<
button
onClick
=
{
incrementBy3
}
>
Increment
by
3
<
/button
>
<
p
>
<
/p
>
<
button
onClick
=
{()
=>
increment
(
3
)}
>
Increment
by
3
<
/button
>
<
/
div
>
<
/
p
>
)
<
/
>
}
)
}
}
const
mapState
=
(
state
)
=>
({
export
default
CounterDisplay
counter
:
state
.
counter
,
})
const
mapDispatch
=
({
counter
:
{
increment
,
incrementAsync
}
})
=>
({
incrementBy3
:
()
=>
increment
(
3
),
})
export
default
connect
(
mapState
,
mapDispatch
)(
CounterDisplay
)
examples/with-rematch/shared/components/header.js
View file @
7ec57c6c
import
React
,
{
Component
}
from
'react'
import
React
from
'react'
import
Link
from
'next/link'
class
Header
extends
Component
{
const
Header
=
()
=>
{
render
()
{
return
(
return
(
<
div
>
<
div
>
<
nav
>
<
nav
>
<
ul
>
<
ul
>
<
li
>
<
li
>
<
a
href
=
"/"
>
Home
<
/a
>
<
Link
href
=
"/"
passHref
>
<
/li
>
<
a
>
Home
<
/a
>
<
li
>
<
/Link
>
<
a
href
=
"/github-users"
>
Async
Example
<
/a
>
<
/li
>
<
/li
>
<
li
>
<
/ul
>
<
Link
href
=
"/github-users"
passHref
>
<
/nav
>
<
a
>
Async
Example
<
/a
>
<
/div
>
<
/Link
>
)
<
/li
>
<
/ul
>
<
/nav
>
<
/div
>
)
}
}
}
export
default
Header
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
state
:
0
,
// initial state
reducers
:
{
reducers
:
{
// handle state changes with pure functions
// handle state changes with pure functions
increment
(
state
,
payload
)
{
increment
:
(
state
,
payload
)
=>
{
if
(
typeof
payload
===
'object'
)
return
state
+
1
return
state
+
payload
return
state
+
payload
},
},
},
},
...
@@ -14,6 +17,6 @@ const counter = {
...
@@ -14,6 +17,6 @@ const counter = {
this
.
increment
(
payload
)
this
.
increment
(
payload
)
},
},
},
},
}
}
)
export
default
counter
export
default
counter
examples/with-rematch/shared/models/github.js
View file @
7ec57c6c
...
@@ -26,6 +26,7 @@ const github = {
...
@@ -26,6 +26,7 @@ const github = {
const
response
=
await
fetch
(
'https://api.github.com/users'
)
const
response
=
await
fetch
(
'https://api.github.com/users'
)
const
users
=
await
response
.
json
()
const
users
=
await
response
.
json
()
this
.
receiveUsers
(
users
)
this
.
receiveUsers
(
users
)
return
users
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
log
(
err
)
console
.
log
(
err
)
this
.
receiveUsers
([])
this
.
receiveUsers
([])
...
...
examples/with-rematch/shared/utils.js
View file @
7ec57c6c
// Robust way to check if it's Node or browser
// Robust way to check if it's Node or browser
import
{
useDispatch
}
from
'react-redux'
export
const
checkServer
=
()
=>
{
export
const
checkServer
=
()
=>
{
return
typeof
window
===
'undefined'
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