Created
October 11, 2019 21:00
-
-
Save mariano-aguero/e787c09c3257a13ec8a0e3ea340b4043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useAsyncDerivedValue = (value, f) => { | |
const [derivedValue, setDerivedValue] = useState(null) | |
const [debouncedValue] = useDebounce(value, 500) | |
useEffect(() => { | |
let cancelled = false | |
f(debouncedValue).then(result => { | |
if (!cancelled) setDerivedValue (result) | |
}) | |
return () => { cancelled = true } | |
}, [debouncedValue]) | |
return derivedValue | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Debounce => https://github.com/xnimorz/use-debounce |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment