Web Development React.js Subjective
Sep 28, 2025

What is the useEffect hook and when is it used?

Detailed Explanation

useEffect is used for side effects in functional components like API calls, subscriptions, or DOM manipulation. It runs after render and can be controlled with dependency arrays.

import { useEffect, useState } from "react";

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  
  useEffect(() => {
    fetchUser(userId).then(setUser);
  }, [userId]); // Runs when userId changes
  
  return <div>{user?.name}</div>;
}
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback