I have an event listener that dispatches an action.
window.addEventListener('resize', () => {
store.dispatch(screenResize());
})
I am trying to throttle (or debounce) this with lodash
The question is, should I do
const throttledScreenResize = _.throttle(screenResize(), 250)
window.addEventListener('resize', () => {
store.dispatch(throttledScreenResize);
})
or
const throttledScreenResize = _.throttle(() => store.dispatch(screenResize()), 250)
window.addEventListener('resize', throttledScreenResize)
or neither? Then what?
Thank you
Take the 2nd approach:
Call the store.dispatch(..)
inside the _throttle
. This will make sure that the store.dispatch
is executed not more than once every 250 ms
const throttledScreenResize = _.throttle(() => store.dispatch(screenResize()), 250)
window.addEventListener('resize', throttledScreenResize)
In the 1st approach: store.dispatch
is called on every resize
event.