The React LifeCycle - Simple Language
To mitro aaj hm fir haazir h apki sewa m , Chaliye mitro aaj hm React LifeCycle ka Funda samjhte hai or doubt clear krte hai, aakhir ye life cycle ka funda h kya , bahut baar interview m bhi pucha jata h.
React ka Component Lifecycle ek tarah ka journey hai jo har React component ko through karna padta hai jab woh create hota hai, update hota hai, ya destroy hota hai. Isme kuch steps hote hain jaise ki:
Initialization (Pehli Mulakat): Jab component create hota hai, toh constructor() function chalta hai, jisme initial state aur props set hote hain.
Mounting (Pahad par chadhna): Yeh phase jab component pehli baar DOM mein render hota hai. Isme do functions chalte hain: componentWillMount() aur componentDidMount(). Pehla function render se pehle chalta tha, dusra render hone ke baad.
Updating (Naye Rang Dikhana): Jab koi update hota hai, naye props ya state aate hain toh component re-renders hota hai. Yahaan shouldComponentUpdate(), componentWillUpdate(), aur componentDidUpdate() functions hote hain jo is process mein madad karte hain.
Unmounting (Alvida Kehna): Jab component DOM se remove hota hai, tab componentWillUnmount() function chalta hai jisme cleanup ki activities hoti hain jaise ki event listeners ya subscriptions ko remove karna.
Error Handling (Galti se Bhool): Agar koi error aata hai during rendering, toh componentDidCatch() function error ko catch karke fallback UI render karta hai.
In sab phases mein, developers ko functions provide kiye gaye hain jo specific actions execute karne ke liye use hote hain. Yeh saare steps ek component ke life cycle ko define karte hain aur inki madad se developers component ke behavior ko control kar sakte hain as per requirement.