Skip to main content
Version: 5.0.3

useTimeout

About#

setTimeout hook for react.

Installation#

npm install --save rooks

Importing the hook#

import { useTimeout } from 'rooks';

Usage#

function TimeoutComponent() {
function doAlert() {
window.alert('timeout expired!');
}
const { start, clear } = useTimeout(doAlert, 2000);
return (
<>
<button onClick={start}> Start timeout </button>
<button onClick={clear}> Clear timeout </button>
</>
);
}
render(<TimeoutComponent />);

Arguments#

ArgumentsTypeDescriptionDefault value
callbackfunctionFunction to be executed in timeoutundefind
delayNumberNumber in milliseconds after which callback is to be run0

Returned Object keys#

Returned object attributesTypeDescription
clearfunctionClear the timeout
startfunctionStart the timeout
isActivebooleanIs the timeout active

Codesandbox Example#

Basic Usage#

Join Bhargav's discord server#

You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.

Last updated on by imbhargav5