debounce-action

Create a debounced Redux action

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
debounce-action
1410.1.05 years ago6 years agoMinified + gzip package size for debounce-action in KB

Readme

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