useEffectOnceWhen

About

Runs a callback effect atmost one time when a condition becomes true

Examples

import { useState } from "react";
import { useEffectOnceWhen } from "rooks";

const App = () => {
  const [loading, setLoading] = useState(true);

  useEffectOnceWhen(() => {
    setTimeout(() => {
      setLoading(false);
    }, 3000); // Countdown for 3 sec
  }, loading);

  return (
    <div>
      <h1>Rooks: useEffectOnceWhen Example</h1>
      <hr></hr>

      <p style={{ fontSize: "20px" }}>
        {loading
          ? "Loading Component (will be gone in 3 secs)...."
          : "Counter Component"}
      </p>
    </div>
  );
};

export default App;

Arguments

ArgumentsTypeDescriptionDefault value
callbackfunctionThe callback to be calledundefined
whenbooleanThe condition which needs to be truetrue

Return

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