Skip to content

Instantly share code, notes, and snippets.

@pedroparra
Last active January 3, 2016 09:25
Show Gist options
  • Select an option

  • Save pedroparra/9e4bfa36854472d14c7c to your computer and use it in GitHub Desktop.

Select an option

Save pedroparra/9e4bfa36854472d14c7c to your computer and use it in GitHub Desktop.
Ejemplo de función reductora en Redux.
// Reducer - Función Pura
// Usando es6
// Nunca cambiamos el state
// Devolvemos 0 si el state es undefined
// Devolvemos el state si no conocemos el action
const myReducer = (state = 0, action) => {
switch(action.type) {
case 'sumar':
return state + 1;
case 'restar':
return state - 1;
default:
return state;
}
}
// Ejemplo del paso de actions al reducer.
// Pasamos el state y el action a realizar.
myReducer(0, {type: 'sumar'}); // -> state = 1
myReducer(1, {type: 'restar'}); // -> state = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment