useInterval
useInterval is a custom hook to execute a callback at a specified interval.
Usage
import { useState } from "react";
import { useInterval } from "hookify-react";
export default function UseIntervalExample() {
const [count, setCount] = useState(0);
const { clear } = useInterval(() => setCount((prev) => prev + 1), 1000);
return (
<div>
<p>Counter: {count}</p>
<button onClick={clear}>Stop Timer</button>
</div>
);
}
API Reference
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callback | () => void | - | Function to be executed at each interval. |
interval | number | 1000 | Time in milliseconds between executions. |
Behavior
- Executes the
callbackfunction at the specifiedinterval. - Returns a
clearfunction to stop the interval manually. - Automatically clears the interval when the component unmounts.
Return Value
useInterval returns an object with the following method:
| Property | Type | Description |
|---|---|---|
clear | () => void | Stops the interval when called. |