Skip to content

Instantly share code, notes, and snippets.

@PsyGik
Created July 8, 2021 18:07
Show Gist options
  • Save PsyGik/3ebe25db6683149b1477995681d7f761 to your computer and use it in GitHub Desktop.
Save PsyGik/3ebe25db6683149b1477995681d7f761 to your computer and use it in GitHub Desktop.
Simple debounce hook
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