mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
43 lines
771 B
Markdown
43 lines
771 B
Markdown
# `useInterval`
|
|
|
|
React hook that allow you using declarative `setInterval`.
|
|
|
|
## Usage
|
|
|
|
```jsx
|
|
import * as React from 'react';
|
|
import {useInterval} from 'react-use';
|
|
|
|
const Demo = () => {
|
|
const [count, setCount] = React.useState(0);
|
|
const [delay, setDelay] = React.useState(1000);
|
|
|
|
useInterval(() => {
|
|
setCount(count + 1);
|
|
}, delay);
|
|
|
|
function handleDelayChange(e) {
|
|
setDelay(Number(e.target.value));
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<div>
|
|
delay: <input value={delay} onChange={handleDelayChange} />
|
|
</div>
|
|
<h1>count: {count}</h1>
|
|
<div>
|
|
<button onClick={() => setDelay(delay ? null : 1000)}>{delay ? 'stop' : 'start'}</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
```
|
|
|
|
|
|
## Reference
|
|
|
|
```js
|
|
useInterval(fn, delay?: number)
|
|
```
|