Skip to content

Instantly share code, notes, and snippets.

@Oscarz90
Last active June 18, 2020 18:21
Show Gist options
  • Select an option

  • Save Oscarz90/b5d34c84c46d1fffedc4532aef4fe1ee to your computer and use it in GitHub Desktop.

Select an option

Save Oscarz90/b5d34c84c46d1fffedc4532aef4fe1ee to your computer and use it in GitHub Desktop.

Revisions

  1. Oscarz90 revised this gist Jun 18, 2020. 1 changed file with 28 additions and 30 deletions.
    58 changes: 28 additions & 30 deletions redux.js
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,43 @@
    import { createStore } from 'redux';
    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.
    * This is a reducer, a pure function with (state, action) => state signature.
    * It describes how an action transforms the state into the next state.
    *
    * 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ó.
    * 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.
    *
    * En este ejemplo, usamos `switch` y strings, pero puedes usar cualquier forma
    * que desees si tiene sentido para tu proyecto.
    * 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;
    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);
    // Create a Redux store holding the state of your app.
    // Its API is { subscribe, dispatch, getState }.
    let store = createStore(counter)

    // Puedes suscribirte manualmente a los cambios, o conectar tu vista
    // directamente
    store.subscribe(() => {
    console.log(store.getState())
    });
    // 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.

    // 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' });
    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' });
    store.dispatch({ type: 'INCREMENT' })
    // 2
    store.dispatch({ type: 'DECREMENT' });
    store.dispatch({ type: 'DECREMENT' })
    // 1
  2. Oscarz90 created this gist Jun 18, 2020.
    45 changes: 45 additions & 0 deletions redux.js
    Original 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