debounce-action

0.1.0 • Public • Published

Debounce Action NPM version

Create debounced redux-thunk actions. Debounced actions will delay being dispatched until after wait milliseconds have elapsed since the last time the debounced action was dispatched.

Installation

npm i --save debounce-action

Usage

import debounceAction from 'debounce-action';
 
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
 
function increment() {
  return {
    type: INCREMENT_COUNTER
  };
}
 
function incrementThunk() {
  return dispatch => {
    dispatch(increment());
  };
}
 
// wrap normal actions with debounceAction() to create debounced actions
const incrementDebounced = debounceAction(increment, 1000);
const incrementThunkDebounced = debounceAction(incrementThunk, 5000, {leading: true});
 
// call debounced actions like normal redux actions
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';
 
const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);
 
store.dispatch(incrementDebounced());
store.dispatch(incrementDebounced());
// --> INCREMENT_COUNTER dispatched once after one second
 
store.dispatch(incrementThunkDebounced());
// --> INCREMENT_COUNTER dispatched immediately
store.dispatch(incrementThunkDebounced());
store.dispatch(incrementThunkDebounced());
// --> INCREMENT_COUNTER dispatched once again after five seconds

Uses lodash's debounce.

License

MIT

Package Sidebar

Install

npm i debounce-action

Weekly Downloads

387

Version

0.1.0

License

MIT

Unpacked Size

6.08 kB

Total Files

7

Last publish

Collaborators

  • jshanson7