react, javascript, frontend

If you want to handle pathological use cases inside React using JavaScript’s setInterval function, use this library called @use-it/interval developed by Donavon.

npm i @use-it/interval

Suppose you want to run a function periodically with a specified interval, but you need to control its behavior dynamically. Here’s how you can do it:

import useInterval from "@use-it/interval";
import { useState } from "react";

function Component() {
    const [play, setPlay] = useState(false);

    useInterval(() => {
        // do something periodically
    }, play ? 1000 * INTERVAL : null);

    // rest of your code ...
}

useInterval() example
In this example, clicking the play button periodically executes another procedure every 1 second to refresh this component in the background (blinks green). As soon as you click pause, it stops running that hook (idle state is gray).

Here’s the codesandbox demo for this.

Similarly, if you want to attach or detach event-handling logic to an element or global scope (like window or document) inside a React Hook, you can use @use-it/event-listener.

How convenient!

Reading list

Well, now what?

You can navigate to more writings from here. Connect with me on LinkedIn for a chat.

  1. 2026

    1. We Might All Be AI Engineers Now
      March 05

      ai, engineering, tools, agents

    2. The Hardest Bug I Ever Fixed Wasn't in Code
      February 07

      engineering, career

    3. Why I Switched to Podman (and Why You Might Too)
      February 02

      docker, tools, linux

  2. 2024

    1. The World is Stochastic
      October 18

      career, philosophy

    2. Debugging a running Java app in Docker
      May 29

      java, docker, debugging

    3. Why is it UTC and not CUT?
      February 21

      time, history

  3. 2023

    1. Deep prop drilling in ReactJS
      December 26

      react, javascript, frontend

    2. Eigenvectors
      October 24

      math, linear-algebra

    3. Java's fork/join framework
      October 21

      java, concurrency

    4. TypeScript's omit and pick
      August 10

      typescript, frontend

    5. JavaScript's new immutable array methods
      June 28

      javascript, frontend

    6. Integrating JUnit 5 in Maven projects
      May 25

      java, testing

    7. My take on ChatGPT and prompt engineering
      March 11

      ai, prompts

    8. Declarative events in ReactJS
      March 09

      react, javascript, frontend

    9. Positive Lookaheads
      March 07

      regex, tools

    10. Functors
      March 06

      functional-programming, math

    11. Fast forward videos with ffmpeg
      January 18

      ffmpeg, tools

    12. Rotate y-axis of a 2D vector
      January 05

      math, vectors

  4. 2022

    1. Synchronizing time
      December 31

      distributed-systems, time

    2. Vector rotation
      November 20

      math, vectors

    3. Sed find and replace
      November 14

      sed, tools, linux

    4. Asgardeo try it application
      September 06

      identity, iam, asgardeo

    5. Flatten error constraints
      August 11

      java, algorithms

    6. Good Git commit messages
      July 24

      git, engineering

    7. Asgardeo JIT user provisioning
      March 09

      identity, iam, asgardeo

    8. Monotonic Arrays
      February 25

      algorithms, javascript

    9. How GOROOT and GOPATH works
      February 01

      go, tooling

  5. 2021

    1. Two summation
      November 21

      algorithms