0.1.0 • Published 6 years ago

debounce-action v0.1.0

Weekly downloads
1,035
License
MIT
Repository
github
Last release
6 years ago

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