Skip to content

Instantly share code, notes, and snippets.

@mariano-aguero
Created October 11, 2019 21:00
Show Gist options
  • Save mariano-aguero/e787c09c3257a13ec8a0e3ea340b4043 to your computer and use it in GitHub Desktop.
Save mariano-aguero/e787c09c3257a13ec8a0e3ea340b4043 to your computer and use it in GitHub Desktop.
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
}
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