Skip to content

Instantly share code, notes, and snippets.

@mozkoq
Last active March 28, 2017 15:02
Show Gist options
  • Save mozkoq/e17127c6863548abf56eff942752aaaf to your computer and use it in GitHub Desktop.
Save mozkoq/e17127c6863548abf56eff942752aaaf to your computer and use it in GitHub Desktop.
redux example
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