mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
ee717af088
This patch removes the global `store` on the client. IMO this example should avoid polluting the global namespace with simple scoping tricks can solve the problem equally as well.
27 lines
797 B
JavaScript
27 lines
797 B
JavaScript
import { createStore, applyMiddleware } from 'redux'
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
|
|
switch (action.type) {
|
|
case 'TICK': return { lastUpdate: action.ts, light: !!action.light }
|
|
default: return state
|
|
}
|
|
}
|
|
|
|
export const startClock = () => dispatch => {
|
|
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
|
|
}
|
|
|
|
let store = null
|
|
|
|
export const initStore = (reducer, initialState, isServer) => {
|
|
if (isServer && typeof window === 'undefined') {
|
|
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
|
|
} else {
|
|
if (!store) {
|
|
store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
|
|
}
|
|
return store
|
|
}
|
|
}
|