mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
useDebounce rerender optimization
This commit is contained in:
parent
454012387b
commit
cab921fdaa
@ -1,13 +1,13 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
const useDebounce = (fn: () => any, ms: number = 0, args: Array<any> = []) => {
|
||||
const [timeout, setTimeoutVar] = useState<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// if args change then clear timeout
|
||||
clearTimeout(timeout);
|
||||
const t = setTimeout(fn.bind(null, args), ms);
|
||||
setTimeoutVar(t);
|
||||
let handle = setTimeout(fn.bind(null, args), ms);
|
||||
|
||||
return () => {
|
||||
// if args change then clear timeout
|
||||
clearTimeout(handle);
|
||||
}
|
||||
}, args);
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user