throttle-action
v0.1.0
Published
Create a throttled Redux action
Downloads
1,234
Maintainers
Readme
Throttle Action
Create throttled redux-thunk actions. Throttled actions can be dispatched at most once per every wait
milliseconds.
Installation
npm i --save throttle-action
Usage
import throttleAction from 'throttle-action';
const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
function increment() {
return {
type: INCREMENT_COUNTER
};
}
function incrementThunk() {
return dispatch => {
dispatch(increment());
};
}
// wrap normal actions with throttleAction() to create throttled actions
const incrementThrottled = throttleAction(increment, 1000);
const incrementThunkThrottled = throttleAction(incrementThunk, 5000, {trailing: false});
// call throttled 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(incrementThrottled());
// --> INCREMENT_COUNTER dispatched immediately
store.dispatch(incrementThrottled());
// --> INCREMENT_COUNTER dispatched again after one second
store.dispatch(incrementThunkThrottled());
// --> INCREMENT_COUNTER dispatched immediately
store.dispatch(incrementThunkThrottled());
store.dispatch(incrementThunkThrottled());
// --> nothing dispatched
Uses lodash's throttle
.
License
MIT