1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-redux/store.js
Dan Zajdband c5d69f0585 Add/move examples (#470)
* Added redux and styled components (wip) examples.

* Updated examples readmes and package.json

* Fixed styled-components example
2016-12-21 21:10:54 -08:00

25 lines
793 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 => {
setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
}
export const initStore = (reducer, initialState, isServer) => {
if (isServer && typeof window === 'undefined') {
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
} else {
if (!window.store) {
window.store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
}
return window.store
}
}