Rooks

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.

On this page