
ReactJS
Every React component goes through phases from creation to removal. Understanding these phases helps you run code at the right time.
Component is being initialized and added to the DOM.
function WelcomeComponent() {
// Component mounted when it appears on screen
return <h1>Welcome!</h1>;
}Component re-renders when props or state change.
function Counter({ increment }) {
const [count, setCount] = React.useState(0);
// Updates when 'increment' prop changes or count state changes
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + increment)}>Increment</button>
</div>
);
}Component is removed from the DOM.
function App() {
const [showComponent, setShowComponent] = React.useState(true);
return (
<div>
{showComponent && <ComponentToRemove />}
<button onClick={() => setShowComponent(!showComponent)}>
{showComponent ? "Remove" : "Show"}
</button>
</div>
);
}āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā MOUNTING PHASE ā
ā 1. Component created ā
ā 2. State initialized (useState) ā
ā 3. First render ā
ā 4. Effects with [] run (useEffect) ā
ā 5. Component ready! ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā UPDATING PHASE ā
ā 1. Props or state changes ā
ā 2. Re-render happens ā
ā 3. Effects with dependencies run ā
ā 4. Previous effect cleanups run first ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā UNMOUNTING PHASE ā
ā 1. Component removed from DOM ā
ā 2. Cleanup functions run ā
ā 3. Component destroyed ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāfunction LifecycleExample() {
const [data, setData] = React.useState(null);
// Mounting: Runs once when component first appears
React.useEffect(() => {
console.log("Component mounted!");
return () => console.log("Component unmounting");
}, []);
// Updating: Runs when data dependency changes
React.useEffect(() => {
console.log("Data updated:", data);
}, [data]);
return <div>{data}</div>;
}| Phase | What Happens | Common Uses |
|---|---|---|
| Mounting | Component appears | Fetch data, initialize state |
| First Update | Initial props arrive | Set initial data from props |
| Subsequent Updates | Props/state change | Update UI based on changes |
| Unmounting | Component removed | Cleanup (timers, listeners) |
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward