Skip to content

Instantly share code, notes, and snippets.

@donabrams
Created August 24, 2016 18:33
Show Gist options
  • Save donabrams/c6932361bc63d888a545c5c1805724a4 to your computer and use it in GitHub Desktop.
Save donabrams/c6932361bc63d888a545c5c1805724a4 to your computer and use it in GitHub Desktop.
Redux Reducer Middleware to Conditionally Execute a second action
import {createStore} from 'redux';
import {interceptingAction} from 'redux-intercepting-action';
import {createAction} from 'redux-actions';
import reducer from './reducers'
const getInterceptingAction = (state)=> {
if (state.good) {
return {
type: 'YAY'
}
}
}
const store = createStore(interceptingAction(getInterceptingAction, reducer), initialState)
export function interceptingAction(getInterceptingAction, reduce) => (state, action) => {
const newState = reduce(state, action)
const secondAction = getInterceptingAction(newState)
return secondAction
? reduce(newState, secondAction)
: newState
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment