Skip to content

Instantly share code, notes, and snippets.

@TheBox193
Created May 17, 2017 17:41
Show Gist options
  • Save TheBox193/634be9e66f7731de6d6c9afcd4c7647a to your computer and use it in GitHub Desktop.
Save TheBox193/634be9e66f7731de6d6c9afcd4c7647a to your computer and use it in GitHub Desktop.
Redux Middleware that tracks a history of dispatched actions for debugging.
const redux_log = [];
const errorLogMiddleware = (store) => (next) => (action) => {
const type = (action && action.type) || 'unknown action';
if ( type === 'LOG_ERROR' ) {
const log_str = redux_log.join(' -> ');
const error_str = (action && action.payload && action.payload.error) || 'Redux Error';
console.error(error_str + ': ' + log_str);
} else {
// add last action to log
redux_log.push(type);
if (redux_log.length > 10) {
redux_log.shift();
}
}
return next(action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment