useTimeoutWhen

About

Takes a callback and fires it when a condition is true

Examples

Basic usage

import "./styles.css";
import { useTimeoutWhen } from "rooks";
import { useState } from "react";

function App() {
  const [start, setStart] = useState(false);
  useTimeoutWhen(() => setStart(false), 2000, start);
  return (
    <>
      <h1>Rooks: useTimeoutWhen example</h1>
      <hr></hr>
      <p>Click the button below to disable it for 2 seconds</p>
      <button onClick={() => setStart(true)} disabled={start}>
        Start timeout
      </button>
    </>
  );
}

export default App;

Arguments

ArgumentsTypeDescriptionDefault value
callbackfunctionFunction to be executed in timeoutundefind
delayNumberNumber in milliseconds after which callback is to be run0
whenbooleanThe condition which when true, sets the timeouttrue

Returned

No return value.

Join the community!

Join our discord server! You can click the floating discord icon at the bottom right of the screen and chat with us!

Powered by vercel