1.0.3 • Published 7 years ago

redux-middleware-injector v1.0.3

Weekly downloads
20
License
-
Repository
github
Last release
7 years ago

Redux Middleware Injector

npm version npm downloads Standard - JavaScript Style Guide

The recomended place to execute actions with side-effects in Redux is in the Middlewares. With this lib you can write your own middleware wherever you want, for example in the action creators file. You don't need to import all the middlewares with applyMiddleware before create the store.

Just import the injector:

import injectMiddleware from "redux-middleware-injector"

const store = createStore(rootReducer, applyMiddleware(injectMiddleware))

And then you can write your own middlewares like the example below:

export const getUser = username => store => {
  const {dispatch} = store

  return next => action => {
    dispatch({type: 'modules/Github/FETCHING'})

    return axios.get(`https://api.github.com/users/${username}`).then(res => {
      dispatch({
        type: 'modules/Github/SUCCESS',
        payload: {
          data: res.data
        }
      })
    })
  }
}

See Redux documentation about Middlewares