
ReactJS
The mounting phase is when your component is created and added to the DOM for the first time.
1. Component function is called
2. State hooks (useState) initialize
3. Component renders (JSX is processed)
4. DOM is updated
5. Effects with [] runfunction UserProfile({ userId }) {
const [user, setUser] = React.useState(null);
const [loading, setLoading] = React.useState(true);
// This effect runs ONCE when component mounts
React.useEffect(() => {
console.log("Component mounted, fetching user...");
fetchUser(userId).then(userData => {
setUser(userData);
setLoading(false);
});
}, []); // Empty dependency array = run on mount only
if (loading) return <div>Loading...</div>;
return <div>{user.name}</div>;
}function MountingOrder() {
console.log("1. Function called");
const [count, setCount] = React.useState(0);
console.log("2. State initialized");
React.useEffect(() => {
console.log("5. Effect ran (mounted)");
}, []);
console.log("3. Render logic executed");
return <div>{count}</div>; // "4. Component renders"
}
// Console output:
// 1. Function called
// 2. State initialized
// 3. Render logic executed
// 4. Component renders
// 5. Effect ran (mounted)function DataFetcher() {
const [data, setData] = React.useState(null);
React.useEffect(() => {
fetch("/api/data")
.then(res => res.json())
.then(data => setData(data));
}, []);
return <div>{data?.title}</div>;
}function WindowListener() {
const [width, setWidth] = React.useState(window.innerWidth);
React.useEffect(() => {
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
return <div>Width: {width}px</div>;
}function AnimatedElement() {
const [isVisible, setIsVisible] = React.useState(false);
React.useEffect(() => {
// Trigger animation after mount
setIsVisible(true);
}, []);
return <div className={isVisible ? "visible" : "hidden"}>Content</div>;
}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