Add ability to debounce window resize updates#4
Add ability to debounce window resize updates#4camflan wants to merge 4 commits intorehooks:masterfrom
Conversation
This allows a user to pass a debounceMs when calling useWindowSize in order to debounce the window resize events
|
please don't include formatting changes with your PR |
|
you're still changing the formatting with indentation |
|
@jamiebuilds I'm working on it :) It certainly wasn't my intention to change your formatting. the first commit was through code sandbox and it applied prettier automatically |
|
any objection to adding a prettier config to maintain your formatting? I'm happy to get one set up in another PR |
|
Sure, could you submit a PR to https://github.com/rehooks/template |
|
What about receiving the debounce function as an argument? |
|
That could be pretty cool to allow a user to pass a function to apply to the event handler before updating state, then they could pick throttle, debounce, filter, etc, etc. However, I'm not sure lodash/debounce works like that. I think debounce takes a fn as the first arg, delay as second. We could specify that they give us a function that will take our fn as an arg? Kind of wonky though. import { debounce as _debounce, throttle as _throttle } from 'lodash/fp';
let debounce = delay => fn => _debounce(fn, delay)
let throttle = delay => fn => _throttle(fn, delay)
function App() {
let windowWidth = useWindowWidth();
let debouncedWindowWidth = useWindowWidth(debounce(500))
let throttledWindowWidth = useWindowWidth(throttle(500))
return [
<pre>Debounced: {JSON.stringify(debouncedWindowWidth)}</pre>,
<pre>Throttled: {JSON.stringify(throttledWindowWidth)}</pre>,
<pre>Firehose: {JSON.stringify(windowWidth)}</pre>,
]
}🤷♂️ Is there a better way? |
|
@camflan the |
|
@camflan @jamesb3ll If you pass a function argument to I mean I guess you could only use the first function ever passed, but that might violate some users' assumption that they can pass a function with a different delay to change throttle/debounce rate. |
|
@camflan to elaborate on what @jamesb3ll said, AFAIK |
This allows a user to pass a debounceMs when calling useWindowSize in order to debounce the window resize events