Lifecycle Hooks
Lifecycle Hooks
Section titled “Lifecycle Hooks”What is React Lifecycle?
Section titled “What is React Lifecycle?”In React:
👉 Lifecycle = the different stages a component goes through from creation -> update -> removal
Modern React Reality (IMPORTANT)
Section titled “Modern React Reality (IMPORTANT)”👉 Earlier (class components):
- lifecycle methods (
componentDidMount, etc.)
👉 Now (functional components):
- Lifecycle is handled using Hooks (
useEffect)
3 Phases of React Lifecycle
Section titled “3 Phases of React Lifecycle”1. Mounting (component created)2. Updating (component changes)3. Unmounting (component removed)1. Mounting Phase (Component Creation)
Section titled “1. Mounting Phase (Component Creation)”👉 When component is first rendered on screen
This is when a component appears on screen for the first time. React runs your component function, builds the virtual DOM, paints to the real DOM, then fires useEffect with an empty [] dependency array. This is your “componentDidMount” equivalent - perfect for fetching initial data, setting up subscriptions, or initializing third-party libraries.
What Happens Internally?
Section titled “What Happens Internally?”- Function runs
- JSX returned
- DOM created
- UI shown
Example
Section titled “Example”function App() { React.useEffect(() => { console.log("Component Mounted"); }, []);
return <h1>Hello</h1>;}Explanation
Section titled “Explanation”useEffect(() => {}, [])👉 Runs ONLY once -> after first render
Interview Line
Section titled “Interview Line”“Mounting is when the component is created and inserted into the DOM. In functional components, we handle it using
useEffectwith an empty dependency array.”
2. Updating Phase (Re-render)
Section titled “2. Updating Phase (Re-render)”👉 Happens when:
Every time state or props change, React re-runs your component function and compares (diffing) the new virtual DOM against the old one. Only the changed parts are updated in the real DOM. useEffect with dependencies fires after every relevant update - React always runs the cleanup of the previous effect before running the new one.
- State changes
- Props change
What Happens Internally?
Section titled “What Happens Internally?”- State/props change
- Component function runs again
- React compares (Virtual DOM)
- Updates only changed parts
Example
Section titled “Example”function Counter() { const [count, setCount] = React.useState(0);
React.useEffect(() => { console.log("Component Updated"); }, [count]);
return ( <button onClick={() => setCount(count + 1)}> {count} </button> );}Explanation
Section titled “Explanation”- When
countchanges -> re-render useEffectruns again because[count]changed
Interview Line
Section titled “Interview Line”“Updating occurs when state or props change. React re-renders the component and updates the DOM efficiently using the Virtual DOM.”
3. Unmounting Phase (Component Removal)
Section titled “3. Unmounting Phase (Component Removal)”👉 When component is removed from DOM
When a component is removed, React runs the cleanup function returned from useEffect. This is where you cancel timers, remove event listeners, or unsubscribe from streams - critical for preventing memory leaks.
What Happens?
Section titled “What Happens?”- Cleanup runs
- Memory cleared
- Subscriptions removed
Example
Section titled “Example”function App() { React.useEffect(() => { console.log("Mounted");
return () => { console.log("Component Unmounted"); }; }, []);
return <h1>Hello</h1>;}Explanation
Section titled “Explanation”- Return function = cleanup
- Runs when component is removed
Interview Line
Section titled “Interview Line”“Unmounting is when the component is removed from the DOM. Cleanup functions inside
useEffectare used to release resources like timers or subscriptions.”
How it works
Section titled “How it works”Full Lifecycle in One Example
Section titled “Full Lifecycle in One Example”function Example({ value }) { const [count, setCount] = React.useState(0);
// Mount + Update React.useEffect(() => { console.log("Mounted or Updated");
return () => { console.log("Cleanup before next run or unmount"); }; }, [count]);
return ( <button onClick={() => setCount(count + 1)}> {count} </button> );}Lifecycle Mapping (VERY IMPORTANT)
Section titled “Lifecycle Mapping (VERY IMPORTANT)”| Phase | Hook Usage |
|---|---|
| Mount | useEffect(() => {}, []) |
| Update | useEffect(() => {}, [deps]) |
| Unmount | return () => {} inside useEffect |
Important Deep Concepts (Interview Level)
Section titled “Important Deep Concepts (Interview Level)”1. useEffect Runs AFTER Render
Section titled “1. useEffect Runs AFTER Render”👉 React first updates UI -> then runs effect
2. Cleanup Runs BEFORE Next Effect
Section titled “2. Cleanup Runs BEFORE Next Effect”Old effect cleanup -> New effect runs3. Multiple useEffects
Section titled “3. Multiple useEffects”useEffect(() => { console.log("Effect 1");}, []);
useEffect(() => { console.log("Effect 2");}, []);👉 They run independently
4. Strict Mode (Important)
Section titled “4. Strict Mode (Important)”In development:
👉 React may run effects twice (for safety)
Class vs Functional Lifecycle (Interview Comparison)
Section titled “Class vs Functional Lifecycle (Interview Comparison)”| Class | Functional |
|---|---|
| componentDidMount | useEffect([], []) |
| componentDidUpdate | useEffect([deps]) |
| componentWillUnmount | cleanup function |
Real-World Example (API Call)
Section titled “Real-World Example (API Call)”function Users() { const [users, setUsers] = React.useState([]);
React.useEffect(() => { fetch("https://api.com/users") .then(res => res.json()) .then(data => setUsers(data)); }, []);
return users.map(user => <p key={user.id}>{user.name}</p>);}👉 Mount -> fetch -> update -> render
Final Mental Model
Section titled “Final Mental Model”Mount -> Show UI -> Run effectUpdate -> Re-render -> Run effectUnmount -> CleanupInterview-Ready Summary
Section titled “Interview-Ready Summary”“React lifecycle has three phases: Mounting, Updating, and Unmounting. In modern React, we manage these using
useEffect. Mounting runs once, updating runs on dependency changes, and unmounting is handled via cleanup functions.”