useEffectEvent
React useEffectEvent hook (React 19.2 stable) se Effect ke andar latest props aur state parhna seekhein, bina Effect ko dobara chalaye, Roman Urdu mein.
On this page
1. useEffectEvent kya hai?
Kabhi Effect ke andar kuch logic reactive hoti hai (jis ke badalne par Effect dobara chalna chahiye) aur kuch non-reactive (jis ki sirf latest value chahiye, lekin uske badalne par Effect restart nahi hona chahiye).
useEffectEvent isi non-reactive hisse ko Effect se alag karta hai. Yeh React 19.2 mein stable hua hai.
2. Kyun zaroori hai?
Socho ek chat room component hai. Connection roomId par depend karta hai. Connect hone par notification dikhani hai jis mein theme use hota hai.
| Sirf useEffect | useEffectEvent ke saath |
|---|---|
| theme ko deps mein daalna parta hai | theme Effect Event ke andar parha jata hai |
| Theme badli to chat reconnect | Theme badli to kuch reconnect nahi |
| Linter warning chupane ka dil karta hai | Linter khush, deps bilkul sahi |
| Stale value ka darr | Hamesha latest theme milti hai |
3. Kahan use hota hai?
| Situation | Reactive hissa | Effect Event mein kya |
|---|---|---|
| Chat connection | roomId | Notification jo theme use kare |
| Page visit analytics | url | cartCount ko log karna |
| Timer ya interval | Interval ka setup | Latest step ke saath state update |
| Event listener | Listener lagana | Handler jo latest props parhe |
4. Kaise kaam karta hai?
Effect Event ko aise samjhein jaise ek event handler, bas fark yeh hai ke yeh user click se nahi, Effect ke andar se fire hota hai.
5. Code example
Chat room notification
import { useEffect, useEffectEvent } from "react";
import { createConnection } from "./chat";
import { showNotification } from "./notifications";
export function ChatRoom({ roomId, theme }: { roomId: string; theme: string }) {
const onConnected = useEffectEvent(() => {
showNotification("Connected!", theme);
});
useEffect(() => {
const connection = createConnection(roomId);
connection.on("connected", () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId]);
return <h1>Room: {roomId}</h1>;
}Analytics with latest value
import { useEffect, useEffectEvent } from "react";
import { logVisit } from "./analytics";
export function Page({ url, cartCount }: { url: string; cartCount: number }) {
const onVisit = useEffectEvent((visitedUrl: string) => {
logVisit(visitedUrl, cartCount);
});
useEffect(() => {
onVisit(url);
}, [url]);
return <main>{url}</main>;
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
useEffectEvent(() => ...) | Non-reactive function banaya |
theme andar parhna | Hamesha latest theme, bina dependency ke |
Deps [roomId] | Sirf room badalne par reconnect |
onVisit(url) | Reactive value argument ke taur par pass ki, clear intent |
cartCount | Sirf parha jata hai, badalne par log dobara nahi hota |
6. Common mistakes
Galati: Effect Event ko dependency array mein daalna
Why? Effect Event jaan boojh kar non-reactive hai, isay deps mein daalne ki zaroorat nahi.
Behtar: Deps mein sirf reactive values rakhein. eslint-plugin-react-hooks ka latest version yeh khud samajhta hai.
Galati: Effect Event ko render ya event handler se call karna
Why? Yeh sirf Effects ke andar se call hone ke liye bana hai, bahar call karna rules ke khilaf hai.
Behtar: Click jaise events ke liye normal event handler function likhein.
Galati: Effect Event ko child component ya dusre hook ko pass karna
Why? Effect Event apne component ke Effects tak mehdood hai, pass karne se behavior unpredictable ho jata hai.
Behtar: Isay usi component mein declare karein jahan ka Effect isay call karta hai.
Galati: Har dependency warning chupane ke liye use karna
Why? Agar logic sach mein reactive hai to usay Effect Event mein daalne se bug aata hai, update miss ho jata hai.
Behtar: Sirf woh logic andar rakhein jiske badalne par Effect restart nahi hona chahiye.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | useEffect, Handling Events, Rules of Hooks |
| Saath | Custom Hooks, useRef |
| Baad mein | useTransition & useDeferredValue, React Compiler |
Final quick revision
| Point | Detail |
|---|---|
| Kya hai | Effect ke andar non-reactive logic ke liye function |
| Version | React 19.2 mein stable |
| Import | import { useEffectEvent } from "react" |
| Latest values | Har call par latest props aur state |
| Deps | Effect Event deps mein nahi jata |
| Call kahan se | Sirf Effects ke andar se |