function type of param with useDebounce return a wrong value
csu-feizao opened this issue · comments
example like
const a = () => 'I am a string'
const debouncedA = useDebounce(a, 500)
console.log(debouncedA)
log result: I am a string
expect: [function]
the reason is when useState
got a function type of param, it will call this function to get result as state
export default function useDebounce<T>(
value: T,
delay: number,
options?: { maxWait?: number; leading?: boolean; trailing?: boolean; equalityFn?: (left: T, right: T) => boolean }
): [T, ControlFunctions] {
const eq = (options && options.equalityFn) || valueEquality;
const [state, dispatch] = useState(value);
...
maybe to change like this will resolve?
export default function useDebounce<T>(
value: T,
delay: number,
options?: { maxWait?: number; leading?: boolean; trailing?: boolean; equalityFn?: (left: T, right: T) => boolean }
): [T, ControlFunctions] {
const eq = (options && options.equalityFn) || valueEquality;
const [state, dispatch] = useState(typeof value === 'function' ? () => value : value);
...