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
胡子大哈 ba54c6ac3d Use the original idea of provider wrapper for redux example (#1201)
Use the original idea of provider wrapper from #1193 and remove
unnecessary `initStore` in every page.
2017-02-18 14:03:02 -03:00

29 lines
870 B
JavaScript

import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import nextConnectRedux from 'next-connect-redux'
export const reducer = (state = { lastUpdate: 0, light: false, title: '' }, action) => {
switch (action.type) {
case 'TICK': return { lastUpdate: action.ts, light: !!action.light, title: state.title }
case 'SET_PAGE_TITLE': return { ...state, title: action.title }
default: return state
}
}
export const setPageTitle = (title) => {
return {
type: 'SET_PAGE_TITLE',
title
}
}
export const startClock = () => dispatch => {
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
}
export const initStore = (initialState) => {
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
}
export const nextConnect = nextConnectRedux(initStore)