redux-timer-middleware
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript
A simple Redux middleware to periodically dispatch actions. Version 1.0.0, no recent updates. Enables starting and stopping named timers via Redux actions, with optional payload, interval, and end-period. No dependencies beyond Redux. Lightweight compared to more complex saga/observable-based solutions.
Common errors
error Uncaught TypeError: Cannot read properties of undefined (reading 'apply') ↓
cause timerMiddleware is a function, but must be applied with applyMiddleware correctly.
fix
import { createStore, applyMiddleware } from 'redux'; createStore(reducer, applyMiddleware(timerMiddleware));
error TypeError: actionName is not a string ↓
cause Missing or non-string actionName in START_TIMER payload.
fix
Ensure payload.actionName is a string like 'MY_ACTION'.
error Uncaught ReferenceError: START_TIMER is not defined ↓
cause Using named import incorrectly or forgetting to import constants.
fix
import { START_TIMER } from 'redux-timer-middleware';
Warnings
gotcha timerName is required for both START_TIMER and STOP_TIMER, but no validation is performed; missing it may cause silent failures. ↓
fix Always provide a unique timerName string.
gotcha timerPeriod counts ticks, not milliseconds; setting timerPeriod: 10 dispatches actionName 10 times (once per interval) then dispatches `actionName + '_END'`. ↓
fix Use timerInterval (in ms) to control tick speed; timerPeriod controls number of ticks.
gotcha The middleware does not clean up timers on store unsubscribe; may cause memory leaks if store is recreated. ↓
fix Manually stop all timers before creating a new store or use a cleanup mechanism.
Install
npm install redux-timer-middleware yarn add redux-timer-middleware pnpm add redux-timer-middleware Imports
- timerMiddleware wrong
import { timerMiddleware } from 'redux-timer-middleware'correctimport timerMiddleware from 'redux-timer-middleware' - START_TIMER wrong
import START_TIMER from 'redux-timer-middleware'correctimport { START_TIMER } from 'redux-timer-middleware' - STOP_TIMER wrong
import STOP_TIMER from 'redux-timer-middleware'correctimport { STOP_TIMER } from 'redux-timer-middleware'
Quickstart
import { createStore, applyMiddleware } from 'redux';
import timerMiddleware, { START_TIMER, STOP_TIMER } from 'redux-timer-middleware';
const reducer = (state = {}, action) => state;
const store = createStore(reducer, applyMiddleware(timerMiddleware));
// Start infinite timer dispatching 'TICK' every second
store.dispatch({
type: START_TIMER,
payload: { actionName: 'TICK', timerName: 'myTimer' }
});
// Later stop it
store.dispatch({
type: STOP_TIMER,
payload: { timerName: 'myTimer' }
});