Last active
September 2, 2017 15:57
-
-
Save arecvlohe/4cab56d099b715544dc46b399379f8fd to your computer and use it in GitHub Desktop.
A simpler Redux boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers, createStore } from "redux"; | |
import Type from "union-type"; | |
// MESSAGES | |
const Msg = Type({ INCREMENT: [], DECREMENT: [], DEFAULT: [] }); | |
// UPDATE | |
const nextState = Msg.caseOn({ | |
INCREMENT: state => state + 1, | |
DECREMENT: state => state - 1, | |
DEFAULT: state => state, | |
_: state => state | |
}); | |
function counter(state = 0, { type = Msg.DEFAULT, payload = null }) { | |
if (typeof type === "string") return state; | |
return nextState(type, state); | |
} | |
// STORE | |
const store = createStore( | |
combineReducers({ | |
counter | |
}), | |
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() | |
); | |
// EXPORTS - ACTIONS & STORE | |
export const add = () => store.dispatch({ type: Msg.INCREMENT }); | |
export const subtract = () => store.dispatch({ type: Msg.DECREMENT }); | |
export default store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment