Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redux 'a lo pobre'

Redux 'a lo pobre'

LimaJS 23/08/2018

Lupo Montero

August 23, 2018
Tweet

More Decks by Lupo Montero

Other Decks in Programming

Transcript

  1. Action reducer() Browser (DOM) render() Store getState() dispatch() subscribe() SignIn

    View View onEvent() onEvent() View View View subscribe() HOC (withStore) App Home View View reducer() dispatch() Action
  2. Principios de Redux • Single source of truth. Everything that

    changes in the app (incl. data and UI state) is contained in a single immutable object called the “state” or the “state tree”. • State is read-only. The state tree is read-only. Anytime you want to change the state you dispatch an action describing what happened. • Changes are made with pure functions. To specify how the state tree is transformed by actions, you write pure reducers.
  3. Store getState: retorna el estado actual dispatch: nos debe permitir

    despachar acciones y actualizar el estado a través de los reducers. subscribe: debe permitirnos suscribir funciones para que se invoquen cuando cambie el estado.
  4. combineReducers export const combineReducers = reducers => (state, action) =>

    Object.keys(reducers).reduce((memo, key) => ({ ...memo, [key]: reducers[key](memo[key], action), }), state || {});
  5. compose export const compose = (...fns) => (...args) => fns.slice(0,

    -1).reverse().reduce( (memo, fn) => fn(memo), fns[fns.length - 1](...args), );
  6. applyMiddleware export const applyMiddleware = (...middlewares) => createStore => (reducers,

    initialState, enhancer) => { const store = createStore(reducers, initialState, enhancer); return { ...store, dispatch: middlewares.slice().reverse().reduce( (dispatch, middleware) => middleware(store)(dispatch), store.dispatch, ), }; };