Note to self

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

Suppose you want to run a function periodically with a specified interval. But you need to control its behavior using another element like this: -

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, suppose you want to attach or detach any event-handling logic to an element or global scope (i.e., window, document) inside a React Hook (i.e., useEffect). You can use @use-it/event-handler.

How convenient!

Reading list

Well, now what?

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