Last active
June 18, 2020 18:21
-
-
Save Oscarz90/b5d34c84c46d1fffedc4532aef4fe1ee to your computer and use it in GitHub Desktop.
Revisions
-
Oscarz90 revised this gist
Jun 18, 2020 . 1 changed file with 28 additions and 30 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,45 +1,43 @@ import { createStore } from 'redux' /** * This is a reducer, a pure function with (state, action) => state signature. * It describes how an action transforms the state into the next state. * * The shape of the state is up to you: it can be a primitive, an array, an object, * or even an Immutable.js data structure. The only important part is that you should * not mutate the state object, but return a new object if the state changes. * * In this example, we use a `switch` statement and strings, but you can use a helper that * follows a different convention (such as function maps) if it makes sense for your * project. */ function counter(state = 0, action) { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } } // Create a Redux store holding the state of your app. // Its API is { subscribe, dispatch, getState }. let store = createStore(counter) // You can use subscribe() to update the UI in response to state changes. // Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly. // However it can also be handy to persist the current state in the localStorage. store.subscribe(() => console.log(store.getState())) // The only way to mutate the internal state is to dispatch an action. // The actions can be serialized, logged or stored and later replayed. store.dispatch({ type: 'INCREMENT' }) // 1 store.dispatch({ type: 'INCREMENT' }) // 2 store.dispatch({ type: 'DECREMENT' }) // 1 -
Oscarz90 created this gist
Jun 18, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ import { createStore } from 'redux'; /** * Esto es un reducer, una función pura con el formato * (state, action) => newState. Describe como una acción transforma el estado * en el nuevo estado. * * La forma del estado depende de tí: puede ser un primitivo, un array, un * objeto, o incluso una estructura de datos de Immutable.js. La única parte * importante es que no debes modificar el objeto del estado, en vez de eso * devolver una nuevo objeto si el estado cambió. * * En este ejemplo, usamos `switch` y strings, pero puedes usar cualquier forma * que desees si tiene sentido para tu proyecto. */ function counter(state = 0, action) { switch (action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } // Creamos un store de Redux almacenando el estado de la aplicación. // Su API es { subscribe, dispatch, getState }. let store = createStore(counter); // Puedes suscribirte manualmente a los cambios, o conectar tu vista // directamente store.subscribe(() => { console.log(store.getState()) }); // La única forma de modificar el estado interno es despachando acciones. // Las acciones pueden ser serializadas, registradas o almacenadas luego para // volver a ejecutarlas. store.dispatch({ type: 'INCREMENT' }); // 1 store.dispatch({ type: 'INCREMENT' }); // 2 store.dispatch({ type: 'DECREMENT' }); // 1