useInterval
Calls a function on a timer, with a null delay to pause it. Cleared on unmount.
Source@monoline/react 0.5.0Markdown for AI toolsstable
- No DOM output. Clears its timer on unmount so nothing runs after the component is gone.
- Pass null as the delay to pause.
Import
import '@monoline/css';
import {useInterval} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add use-interval
Usage
import {useState} from 'react';
import {Button, useInterval} from '@monoline/react';
function IntervalDemo() {
const [running, setRunning] = useState(true);
const [seconds, setSeconds] = useState(0);
useInterval(() => setSeconds((s) => s + 1), running ? 1000 : null);
return (
<div>
<p>Elapsed: {seconds} s</p>
<Button variant="line" onClick={() => setRunning((r) => !r)}>{running ? 'Pause' : 'Resume'}</Button>
</div>
);
}
<IntervalDemo />
Examples
Behaviour
- No DOM output. Clears its timer on unmount so nothing runs after the component is gone.
- Pass null as the delay to pause.