Skip to main content
Version: 5.7.2

useIntervalWhen

About​

Sets an interval immediately when a condition is true

Installation​

npm install --save rooks

Importing the hook​

import { useIntervalWhen } from "rooks";

Usage​

function App() {
const [value, setValue] = useState(0);
const [booleanState, setBooleanState] = useState(true);

useIntervalWhen(
() => {
setValue(value + 10);
},
1000, // run callback every 1 second
booleanState, // start the timer when it's true
true // no need to wait for the first interval
);

return (
<div
style={{ display: "flex", flexDirection: "column", alignItems: "center" }}
>
<h1>Rooks: useIntervalWhen example</h1>
<h2>Value: {value}</h2>
</div>
);
}

Arguments​

ArgumentTypeDescriptionDefault value
callbackfunctionFunction be invoked after each interval durationundefined
intervalDurationnumberDuration in milliseconds after which callback is invoked0
whenbooleanOnly start timer when when is truetrue
startImmediatebooleanShould the timer start immediately or nofalse

Returned Object​

No return value.

Codesandbox Examples​

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.