Course contentsShow
← React
Lesson 681 of 2,39618. When NOT to Use useEffectFree lesson

The Mental Model: Cause and Effect

Understanding that user actions are the cause; effects should respond to data changes, not user intent.

The Mental Model: Cause and Effect

What you'll learn: How to identify the true cause of changes in React and structure your code to respond to the right triggers.

The Core Distinction

In React, user actions are causes, but they're not what effects should respond to. When a user clicks a button, that action causes state to change. Effects should respond to the data changes, not to the user's intent.

Think of it like a row of dominoes: The user pushes the first domino (clicks a button) → state falls over (updates) → effects respond to the state change. You don't attach a sensor to the user's finger; you watch the dominoes.

Why This Matters

Many developers write effects that try to "listen" for user actions:

// ❌ Wrong mental model: Effect reacting to button click
useEffect(() => {
  // This runs after userId changes...
  // but the CAUSE was the user clicking!
  fetchUserData(userId);
}, [userId]);

The event handler is the response to the user action. If logic belongs there, it shouldn't be in an effect:

// ✅ Right: Event handler owns the user action response
const handleLoadUser = () => {
  setUserId(newId);  // Cause: state change
  // Direct consequence of the click
};

The Right Question

Ask yourself: "Is this code responding to the user doing something, or to data changing?"

  • User doing something → Event handler
  • Data changing (from any source) → Effect

Effects are for synchronization with systems outside React (network, browser APIs, subscriptions), not for orchestrating the direct consequences of user intent.

Key Takeaway: User actions are causes that change state; effects respond to those state changes, not to the user's intent. Keep user-action logic in handlers.