Skip to main content

useInterval

A declarative interval hook based on Dan Abramov's article on overreacted.io. The interval can be paused by setting the delay to null

You can also manually control it by passing the controls parameter.

Usage

Live Editor

function Demo() {
  const [count, setCount] = useState(0);

  useInterval(() => {
    setCount(count + 1);
  }, 1000);

  return <div>count: {count}</div>;
};

Result
Loading...

API

useInterval

Returns

Pausable

Arguments

ArgumentDescriptionTypeDefaultValue
callbackcallback() => void (Required)-
delayTime, if null then stop the timernumber | null | undefined-
optionsoptional paramsUseIntervalOptions | undefined-

UseIntervalOptions

PropertyDescriptionTypeDefaultValue
immediateWhether to execute immediately.boolean-
controlsWhether to control execution.boolean-

Pausable

PropertyDescriptionTypeDefaultValue
isActiveA ref indicate whether a pausable instance is activeRefObject<boolean> (Required)-
pauseTemporary pause the effect from executing() => void (Required)-
resumeResume the effects() => void (Required)-