Skip to content

Instantly share code, notes, and snippets.

@loonywizard
Last active September 5, 2023 08:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loonywizard/1c0767462699f29a8a7c9ea3590491de to your computer and use it in GitHub Desktop.
Save loonywizard/1c0767462699f29a8a7c9ea3590491de to your computer and use it in GitHub Desktop.
Example of usage of React useThrottle hook
import React, { useEffect, useState } from 'react'
import { useThrottle } from './useThrottle'
export default function App() {
const [value, setValue] = useState('hello')
const throttledValue = useThrottle(value)
useEffect(() => console.log(`throttledValue changed: ${throttledValue}`), [
throttledValue,
])
function onChange(event: React.ChangeEvent<HTMLInputElement>) {
setValue(event.target.value)
}
return (
<div>
Input: <input value={value} onChange={onChange} />
<p>Throttled value: {throttledValue}</p>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment