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

The Mental Model: Synchronization

Learn to think of effects as synchronizing your component with an external system.

The Mental Model: Synchronization

What you'll learn: How to think of useEffect as keeping your component in sync with external systems, not just "doing things after render."

The Synchronization Mindset

Many beginners think of useEffect as "run some code after rendering." While technically true, this mental model leads to confusion. A better way to think about effects is synchronization: your component needs to stay connected to something outside React.

What Does Synchronization Mean?

Think of a thermostat connected to a heating system. The thermostat doesn't just "turn on the heater once" — it continuously monitors temperature and keeps the heater in the correct state. When you change the target temperature, the thermostat synchronizes the heater with that new setting.

Effects work the same way:

  • You connect to a chat server → you need to disconnect when done
  • You subscribe to browser events → you need to unsubscribe when done
  • You start fetching data → you might need to cancel if props change

Your effect's job is to start the synchronization when the component appears (or when dependencies change) and stop it when appropriate.

Not Just "Do Something Once"

If you only think "run code after render," you'll forget about cleanup. But if you think "keep this subscription active," cleanup becomes obvious: when the component goes away, end the subscription.

This mental shift helps you understand why effects run after every render by default — React is re-synchronizing with potentially changed data.

Key Takeaway: Think of useEffect as synchronizing your component with an external system, not just running side effects. This mindset naturally leads to proper setup and cleanup.