Loading...
Please waitLoading...
Please waitpnpm dlx uselab@latest add use-throttle
import * as React from "react"
import { useThrottle } from "@/hooks/use-throttle"
export function Component() {
const [val, setVal] = React.useState("")
const throttledValue = useThrottle(val, 300)
return (
<section>
<h1>useThrottle</h1>
<input
placeholder="Type some text"
style={{ background: "var(--charcoal)" }}
type="text"
value={val}
onChange={(e) => {
setVal(e.target.value)
}}
/>
<p>Val: {val}</p>
<p>Throttled: {throttledValue}</p>
</section>
)
}const throttledValue = useThrottle(inputValue, 300)inputValue: The value to throttle (e.g., a search query or scroll position).300: The interval in milliseconds (e.g., update at most every 300ms).throttledValue: The throttled value that updates at most once every 300ms.| Name | Type | Description | Default Value | Optional |
|---|---|---|---|---|
value | T | The value to throttle. This can be of any type (string, number, object, etc.). | - | No |
interval | number | The interval in milliseconds at which the throttled value is updated. | 500 | Yes |
| Name | Type | Description |
|---|---|---|
throttledValue | T | The throttled value, updated at most once per specified interval. |