Skip to content

Instantly share code, notes, and snippets.

@markerikson
Last active January 5, 2021 17:37
Show Gist options
  • Save markerikson/ca96a82d6fdb29388aca4052a9455431 to your computer and use it in GitHub Desktop.
Save markerikson/ca96a82d6fdb29388aca4052a9455431 to your computer and use it in GitHub Desktop.
Sample Redux timer middleware
function timerMiddleware({dispatch, getState}) {
const timers = {};
return next => action => {
if(action.type == "START_TIMER") {
const {action, timerName, timerInterval} = action.payload;
clearInterval(timers[timerName]);
timers[timerName] = setInterval( () => {
dispatch({type : action })
}, timerInterval);
}
else if(action.type == "STOP_TIMER") {
const {timerName} = action.payload;
clearInterval(timers[timerName]);
}
else {
return next(action);
}
}
}
@sompylasar
Copy link

action.payload.timerInterval => timerInterval, it has already been destructured above.

@zz-james
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment