https://github.com/hermanya/use-interval
⏲ Dan Abramov's interval hook.
https://github.com/hermanya/use-interval
Last synced: about 1 year ago
JSON representation
⏲ Dan Abramov's interval hook.
- Host: GitHub
- URL: https://github.com/hermanya/use-interval
- Owner: Hermanya
- License: mit
- Created: 2019-02-04T04:05:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:11:01.000Z (over 3 years ago)
- Last Synced: 2025-04-09T20:09:00.982Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/use-interval
- Size: 2.53 MB
- Stars: 68
- Watchers: 2
- Forks: 8
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# use-interval
> React hook for setting an interval as posted on overreacted.io
[](https://www.npmjs.com/package/use-interval) [](https://standardjs.com)
[Dan Abramov's blog post explaining why you cannot just use `setInterval` within `useEffect`.](https://overreacted.io/making-setinterval-declarative-with-react-hooks/)
## Used by
- [codesandbox / codesandbox-client](https://github.com/codesandbox/codesandbox-client)
- [kentcdodds / react-performance](https://github.com/kentcdodds/react-performance)
- [siddharthkp / react-ui](https://github.com/siddharthkp/react-ui)
- [element-motion](https://github.com/element-motion/element-motion)
- [wintercounter / mhy](https://github.com/wintercounter/mhy)
- [sagemathinc / cocalc](https://github.com/sagemathinc/cocalc)
- [wintercounter / mhy](https://github.com/wintercounter/mhy)
## Install
```bash
npm install --save use-interval
```
## Usage
```tsx
import * as React from 'react'
import useInterval from 'use-interval'
const Example = () => {
let [count, setCount] = React.useState(0);
useInterval(() => {
// Your custom logic here
setCount(count + 1);
}, 1000); // passing null instead of 1000 will cancel the interval if it is already running
return
{count}
;
}
```
```tsx
// TypeScript Declaration
useInterval(
callback: () => void,
delay: number,
immediate?: boolean /* called when mounted if true */
)
```
## License
MIT
---
This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).