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
3b1f7115
Commit
3b1f7115
authored
Jan 08, 2017
by
Bernat Orell
Committed by
Arunoda Susiripala
Jan 08, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Routing on the client with Redux (#691)
* Routing on the client with Redux * Removing unused import
parent
92904113
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
10 deletions
+56
-10
Clock.js
examples/with-redux/components/Clock.js
+2
-5
Page.js
examples/with-redux/components/Page.js
+15
-0
package.json
examples/with-redux/package.json
+1
-1
index.js
examples/with-redux/pages/index.js
+3
-3
other.js
examples/with-redux/pages/other.js
+34
-0
store.js
examples/with-redux/store.js
+1
-1
No files found.
examples/with-redux/components/Clock.js
View file @
3b1f7115
import
React
from
'react'
import
{
connect
}
from
'react-redux'
export
default
connect
(
state
=>
state
)(({
lastUpdate
,
light
})
=>
{
export
default
({
lastUpdate
,
light
})
=>
{
return
(
<
div
className
=
{
light
?
'light'
:
''
}
>
{
format
(
new
Date
(
lastUpdate
))}
...
...
@@ -20,7 +17,7 @@ export default connect(state => state)(({ lastUpdate, light }) => {
`
}
<
/style
>
<
/div
>
)
}
)
}
const
format
=
t
=>
`
${
pad
(
t
.
getHours
())}
:
${
pad
(
t
.
getMinutes
())}
:
${
pad
(
t
.
getSeconds
())}
`
...
...
examples/with-redux/components/Page.js
0 → 100644
View file @
3b1f7115
import
Link
from
'next/link'
import
{
connect
}
from
'react-redux'
import
Clock
from
'./Clock'
export
default
connect
(
state
=>
state
)(({
title
,
linkTo
,
lastUpdate
,
light
})
=>
{
return
(
<
div
>
<
h1
>
{
title
}
<
/h1
>
<
Clock
lastUpdate
=
{
lastUpdate
}
light
=
{
light
}
/
>
<
nav
>
<
Link
href
=
{
linkTo
}
>
Navigate
<
/Link
>
<
/nav
>
<
/div
>
)
})
examples/with-redux/package.json
View file @
3b1f7115
...
...
@@ -7,7 +7,7 @@
"start"
:
"next start"
},
"dependencies"
:
{
"next"
:
"
*
"
,
"next"
:
"
^2.0.0-beta
"
,
"react-redux"
:
"^5.0.1"
,
"redux"
:
"^3.6.0"
,
"redux-thunk"
:
"^2.1.0"
...
...
examples/with-redux/pages/index.js
View file @
3b1f7115
import
React
from
'react'
import
{
Provider
}
from
'react-redux'
import
{
reducer
,
initStore
,
startClock
}
from
'../store'
import
Clock
from
'../components/Clock
'
import
Page
from
'../components/Page
'
export
default
class
Counter
extends
React
.
Component
{
static
getInitialProps
({
req
})
{
const
isServer
=
!!
req
const
store
=
initStore
(
reducer
,
null
,
isServer
)
store
.
dispatch
({
type
:
'TICK'
,
ts
:
Date
.
now
()
})
store
.
dispatch
({
type
:
'TICK'
,
light
:
!
isServer
,
ts
:
Date
.
now
()
})
return
{
initialState
:
store
.
getState
(),
isServer
}
}
...
...
@@ -27,7 +27,7 @@ export default class Counter extends React.Component {
render
()
{
return
(
<
Provider
store
=
{
this
.
store
}
>
<
Clock
/>
<
Page
title
=
'Index Page'
linkTo
=
'/other'
/>
<
/Provider
>
)
}
...
...
examples/with-redux/pages/other.js
0 → 100644
View file @
3b1f7115
import
React
from
'react'
import
{
Provider
}
from
'react-redux'
import
{
reducer
,
initStore
,
startClock
}
from
'../store'
import
Page
from
'../components/Page'
export
default
class
Counter
extends
React
.
Component
{
static
getInitialProps
({
req
})
{
const
isServer
=
!!
req
const
store
=
initStore
(
reducer
,
null
,
isServer
)
store
.
dispatch
({
type
:
'TICK'
,
light
:
!
isServer
,
ts
:
Date
.
now
()
})
return
{
initialState
:
store
.
getState
(),
isServer
}
}
constructor
(
props
)
{
super
(
props
)
this
.
store
=
initStore
(
reducer
,
props
.
initialState
,
props
.
isServer
)
}
componentDidMount
()
{
this
.
timer
=
this
.
store
.
dispatch
(
startClock
())
}
componentWillUnmount
()
{
clearInterval
(
this
.
timer
)
}
render
()
{
return
(
<
Provider
store
=
{
this
.
store
}
>
<
Page
title
=
'Other Page'
linkTo
=
'/'
/>
<
/Provider
>
)
}
}
examples/with-redux/store.js
View file @
3b1f7115
...
...
@@ -9,7 +9,7 @@ export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
}
export
const
startClock
=
()
=>
dispatch
=>
{
setInterval
(()
=>
dispatch
({
type
:
'TICK'
,
light
:
true
,
ts
:
Date
.
now
()
}),
800
)
return
setInterval
(()
=>
dispatch
({
type
:
'TICK'
,
light
:
true
,
ts
:
Date
.
now
()
}),
800
)
}
export
const
initStore
=
(
reducer
,
initialState
,
isServer
)
=>
{
...
...
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