Skip to content

Instantly share code, notes, and snippets.

@halan
Created December 2, 2016 18:06
Show Gist options
  • Save halan/778a29c418d577b537c918b7dac66bea to your computer and use it in GitHub Desktop.
Save halan/778a29c418d577b537c918b7dac66bea to your computer and use it in GitHub Desktop.
import { createStore } from 'redux'
const Pokemon = {
init: (specie) => ({ level: 1, specie }),
setSpecie: (prev, specie) => ({ ...prev, specie }),
updateLevel: (prev) => ({ ...prev, level: prev.level + 1})
}
const eevee = Pokemon.init('Eevee');
const eeveelution = (state = eevee, action) => {
switch (action.type){
case 'WATER_STONE':
return Pokemon.setSpecie(state, 'Vaporeon');
case 'THUNDER_STONE':
return Pokemon.setSpecie(state, 'Jolteon');
case 'FIRE_STONE':
return Pokemon.setSpecie(state, 'Flareon');
case 'LEVEL_UP':
return Pokemon.updateLevel(state);
default:
return state;
}
}
const store = createStore(eeveelution);
store.subscribe(() => console.log(store.getState()));
console.log(store);
store.dispatch({ type: 'WATER_STONE' });
store.dispatch({ type: 'LEVEL_UP' });
store.dispatch({ type: 'THUNDER_STONE' });
store.dispatch({ type: 'WATER_STONE' });
console.log(eevee);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment