Created
July 8, 2021 18:07
-
-
Save PsyGik/3ebe25db6683149b1477995681d7f761 to your computer and use it in GitHub Desktop.
Simple debounce hook
This file contains 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
import React, { useState, useEffect } from 'react'; | |
export default (value, timeout) => { | |
const [state, setState] = useState(value); | |
useEffect(() => { | |
const handler = setTimeout(() => setState(value), timeout); | |
return () => clearTimeout(handler); | |
}, [value, timeout]); | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment