Last active
January 3, 2016 09:25
-
-
Save pedroparra/9e4bfa36854472d14c7c to your computer and use it in GitHub Desktop.
Ejemplo de función reductora en Redux.
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
| // 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