Closed
Description
I found debounce
setting trailing
to `false unexpectedly in this code. The react is not part of the problem, but I'm including it for you to understand why I destructured the object in the first place:
import { useState, useRef, useEffect } from 'react'
import { debounce, DebouncedFunc, DebounceSettings } from 'lodash'
export default function useDebounce(value, delay, { leading, trailing, maxWait }): {
useEffect(() => {
const dbFn = debounce(setDebounced, delay, { leading, trailing, maxWait })
...
}, [delay, leading, maxWait, trailing])
What ends up happening here is that trailing
is getting set to false
because of https://github.com/lodash/lodash/blob/master/debounce.js#L89
trailing = true
if (isObject(options)) {
trailing = 'trailing' in options ? !!options.trailing : trailing
}
I expected that if I didn't pass a boolean to trailing
that the default would be used. However { trailing: undefined }
is equivalent to { trailing: false }
which happens sneakily when you use object-shorthand.
Could lodash instead use trailing = options.trailing !== undefined ? !!options.trailing : trailing
, or equivalent vs the in
operator?