Course contentsShow
← React
Lesson 532 of 2,39615. useEffect FundamentalsFree lesson

Multiple Effects in One Component

Learn that you can use multiple useEffect calls to separate concerns and organize related logic.

Multiple Effects in One Component

What you'll learn: You can use useEffect multiple times in a single component to keep different side effects organized and separate.

Why Multiple Effects?

Just like you can call useState multiple times for different pieces of state, you can call useEffect multiple times for different side effects. This is actually recommended — it keeps your code cleaner and easier to understand.

Think of it like organizing your kitchen: you wouldn't throw all your utensils, dishes, and food into one drawer. You separate things by purpose. Effects work the same way.

The Pattern

Each effect handles one concern:

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  const [posts, setPosts] = useState([]);

  // Effect #1: Fetch user data
  useEffect(() => {
    fetch(`/api/users/${userId}`)
      .then(res => res.json())
      .then(data => setUser(data));
  });

  // Effect #2: Update document title
  useEffect(() => {
    document.title = user ? `${user.name}'s Profile` : 'Loading...';
  });

  // Effect #3: Log analytics
  useEffect(() => {
    console.log('Profile viewed:', userId);
  });

  return <div>{/* ... */}</div>;
}

Each effect runs after every render, but they run independently. One effect doesn't block or interfere with another.

Benefits of Separation

  • Easier to read: Each effect has a single, clear purpose
  • Easier to maintain: You can modify one effect without touching others
  • Better organization: Related logic stays together

Key Takeaway: Use multiple useEffect calls to separate different concerns — don't cram unrelated side effects into one massive effect. Each effect should handle one logical piece of work.