Last active
March 28, 2017 15:02
-
-
Save mozkoq/e17127c6863548abf56eff942752aaaf to your computer and use it in GitHub Desktop.
redux example
This file contains hidden or 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
const { createStore, combineReducers } = require('redux') | |
const ActionType = { | |
Inc: Symbol(), | |
Dec: Symbol(), | |
Pow: Symbol(), | |
} | |
const inc = () => | |
({ type: ActionType.Inc }) | |
const dec = () => | |
({ type: ActionType.Dec }) | |
const pow = n => | |
({ type: ActionType.Pow, payload: n }) | |
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case ActionType.Inc: | |
return state + 1 | |
case ActionType.Dec: | |
return state - 1 | |
case ActionType.Pow: | |
return state ** action.payload | |
default: | |
return state | |
} | |
} | |
const reducer = combineReducers({ counter }) | |
const store = createStore(reducer) | |
store.dispatch(inc()) | |
store.dispatch(inc()) | |
store.dispatch(inc()) | |
store.dispatch(pow(3)) | |
console.log(store.getState()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment