npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-timeout

v1.0.0

Published

React hooks for setTimeout and setInterval.

Downloads

12,185

Readme

react-use-timeout

React hooks that wrapper standard JS setTimeout and setInterval.

Why would I use this?

Helps prevent memory leaks.

Normally you would have to remember to clear your unfinished timeouts before a component is detached. For example to manage a simple timeout:

import React, {useEffect, useRef} from 'react';

export default function Example() {
  // save off the id of the timeout so we can cancel it later.
  const timeout = useRef();

  useEffect(() => {
    // start timeout
    timeout.current = setTimeout(doSomething, 30000);

    // cleanup function to cancel the timeout if it hasn't finished.
    return () => {
      if (timeout.current) {
        clearTimeout(timeout.current);
      }
    };
  }, []);

  return <div />
}

If you forget to return a cleanup function from the useEffect that clears the timeout, then it could cause a memory leak (depending on what variables are held in the closure) or cause a react warning if the callback changes state on the component but the component is unmounted befire the timeout fires.

Less code

Instead of the above example of using a setTimeout, this is the same code with the hook:

import React, {useEffect} from 'react';
import {useTimeout} from 'react-use-timeout';

export default function Example() {
  const timeout = useTimeout(doSomething, 30000);

  useEffect(() => {
    timeout.start();
  }, []);

  return <div />
}

Mockable for testing

It is easier to mock these hooks then to mock setTimeout.

Examples in Jest:

// Mock the useTimeout hook to execute all timeouts at 0ms
jest.mock('react-use-timeout', () => {
  return {
    useTimeout: jest.fn(callback => setTimeout(callback, 0)),
  };
});
// Mock the hooks to never execute timeouts
jest.mock('react-use-timeout', () => {
  return {
    useTimeout: () => {},
    useInterval: () => {},
  };
});

How to Use

You can install this library using

npm add react-use-timeout

or

yarn add react-use-timeout

Then you can import it into your component

import {useTimeout, useInterval} from 'react-use-timeout';

useTimeout

Call the hook to get an instance of a timeout. You pass a callback to execute, and optionally a timeout to execute it at (the default is 0ms). Note that the timeout does not start yet. You need to call .start() on the returned object.

The returned timeout instance provides 2 functions:

  • .start(timeout) : Start the timeout. Optionally you can pass a new timeout duration. If passed, this will override the duration passed to useTimeout()
  • .stop() : Cancels the timeout.
import React, {useEffect, useCallback, useState} from 'react';
import {useTimeout} from 'react-use-timeout';

// This component would change it's text color from green to red 10 seconds after it was mounted.
export default function Example() {
  const [color, setColor] = useState('green');

  const changeColor = useCallback(() => {
    setColor('red');
  });

  const timeout = useTimeout(changeColor, 10000);

  useEffect(() => {
    timeout.start();
  }, []);

  return <div style={{color}} />
}

useInterval

Call the hook to get an instance of an interval. You pass a callback to execute, and optionally a interval to execute it at. Note that the interval does not start yet. You need to call .start() on the returned object.

The returned interval instance provides 2 functions:

  • .start(timeout) : Start the intrval. Optionally you can pass a new timeout duration. If passed, this will override the duration passed to useInterval()
  • .stop() : Cancels the interval.
import React, {useEffect, useCallback, useState} from 'react';
import {useInterval} from 'react-use-timeout';

// This component would alternate it's color between green and red each 1 second after it was mounted.
export default function Example() {
  const [color, setColor] = useState('green');

  const toggleColor = useCallback(() => {
    setColor(color === 'green' ? 'red', 'green');
  });

  const interval = useInterval(toggleColor, 1000);

  useEffect(() => {
    interval.start();
  }, []);

  return <div style={{color}} />
}