mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Updated Redux example (markdown)
parent
712990f973
commit
1ae96eaa2a
|
@ -21,7 +21,7 @@ export default class Counter extends React.Component {
|
|||
static getInitialProps ({ req }) {
|
||||
const isServer = !!req
|
||||
const store = initStore(reducer, null, isServer)
|
||||
store.dispatch({ type: 'TICK' })
|
||||
store.dispatch({ type: 'TICK', ts: Date.now() })
|
||||
return { initialState: store.getState(), isServer }
|
||||
}
|
||||
|
||||
|
@ -58,13 +58,13 @@ import thunkMiddleware from 'redux-thunk'
|
|||
|
||||
export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
|
||||
switch (action.type) {
|
||||
case 'TICK': return { lastUpdate: Date.now(), light: !!action.light }
|
||||
case 'TICK': return { lastUpdate: action.ts, light: !!action.light }
|
||||
default: return state
|
||||
}
|
||||
}
|
||||
|
||||
export const startClock = () => dispatch => {
|
||||
setInterval(() => dispatch({ type: 'TICK', light: true }), 800)
|
||||
setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
|
||||
}
|
||||
|
||||
export const initStore = (reducer, initialState, isServer) => {
|
||||
|
|
Loading…
Reference in a new issue