
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
react-use-timeout
Advanced tools
React hooks that wrapper standard JS setTimeout and setInterval.
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.
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 />
}
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: () => {},
};
});
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';
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}} />
}
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}} />
}
FAQs
React hooks for setTimeout and setInterval.
The npm package react-use-timeout receives a total of 4,517 weekly downloads. As such, react-use-timeout popularity was classified as popular.
We found that react-use-timeout demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.