react-useinterval
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

npm version GitHub license

react-useInterval

A custom React hook that wraps JavaScript's setInterval function.

Install

Install with npm (or yarn):

$ npm install --save react-useinterval

API

useInterval(callback, delay, ...args)

Property Type Required Description
callback Function Yes A function to be executed every delay milliseconds.
delay Number, undefined, or null No The time, in milliseconds, that the timer should delay in between executions of the specified function. Note: If undefined or null is passed, the interval will be paused.
...args Any No Additional arguments which are passed through to the function specified by callback.

Example Usage

This creates a counter that counts up by five every second.

import React, { useState } from 'react';
import useInterval from 'react-useinterval';
 
function Counter() {
  let [count, setCount] = useState(0);
 
  const increaseCount = amount => {
    setCount(count + amount);
  };
 
  useInterval(increaseCount, 1000, 5);
  return <h1>{count}</h1>;
}

Thanks

Inspired by Dan Abramov's blog post here.

Package Sidebar

Install

npm i react-useinterval

Weekly Downloads

5,053

Version

1.0.2

License

MIT

Unpacked Size

3.87 kB

Total Files

5

Last publish

Collaborators

  • johndiiorio