Lifecycle & Effects
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
| Arguments | Type | Description | Default value |
|---|---|---|---|
| callback | function | The callback to be called | undefined |
| when | boolean | The condition which needs to be true | true |
Return
No return value.