DEV Community

Cover image for What If React Had NO useEffect?
Mohamad Msalme
Mohamad Msalme

Posted on

What If React Had NO useEffect?

What If React Had NO useEffect? Building It From Scratch to Understand the WHY

Know WHY — Let AI Handle the HOW 🤖

Ever wondered why useEffect works the way it does? Let's think through this together by imagining React without useEffect and building our own solution step by step.

🎯 The Foundation: Everything Is Event-Driven

Your entire React app is just responding to events:

  • User clicks something → you handle it
  • API sends data → you handle it
  • Timer fires → you handle it

Simple, right? But here's where it gets interesting...

🧩 The Missing Piece

What if I want to "listen" to my props changing? Or my state updating?

There's no props.addEventListener('change') - props aren't DOM events. They're React's internal updates.

We need a way to subscribe to React's private changes.

🔨 Building Our Solution Step-by-Step

If we designed this subscription system from scratch, we'd need:

1️⃣ A callback function (what to do)

2️⃣ A condition (when to do it)

3️⃣ Timing control (before/after DOM updates)

That's literally what useEffect is!

🪤 The Closure Problem (Here's the WHY)

Now here's where things get really interesting...

When your effect runs, JavaScript creates a "snapshot" of all the values it uses. This is called a closure - it literally closes over those values.

But what happens when those values change in your component?

Your effect is still holding onto the OLD snapshot! It has no idea the values have updated.

💭 The Question This Raises:

How do we tell our effect: "Hey, get a fresh snapshot when these specific values change"?

We need a way to say:

"I want to subscribe to changes in THESE values specifically, and when they change, give me a new closure with the updated values."

✨ That's Exactly What the Dependency Array Solves!

It's like telling React:

"Watch these values for me. When any of them change, throw away my old closure and create a new one with fresh values."

useEffect(() => {
  // This closure captures current values
  console.log(`Current count: ${count}`);
}, [count]); // "Re-create this effect when count changes"
Enter fullscreen mode Exit fullscreen mode

📋 The Three Patterns Explained

Once you understand the subscription model, these patterns make perfect sense:

  • [value1, value2] → "Re-run when these change"
  • [] → "Run once, don't watch anything"
  • No array → "Run every single render" (usually overkill!)

⚡ Why Functions, Not Promises?

Here's a crucial design decision: Why does useEffect take a function instead of returning a Promise?

Because React needs to know RIGHT NOW what cleanup to do when values change.

Promises are async - they'd break React's predictable update flow. React's render cycle is synchronous, and effects need to be too.

🎯 The Real Insight

useEffect isn't some mysterious hook. It's just:

  • A subscription system for React's internal events
  • A way to get fresh closures when dependencies change
  • A cleanup mechanism that runs synchronously with React's lifecycle

Your dependency array IS your subscription criteria.

Once you see this, the "exhaustive dependencies" rule makes perfect sense - you're just telling React exactly what to watch!

🧠 The Takeaway

Understanding the WHY behind useEffect transforms how you think about:

  • Side effects in React
  • Dependency management
  • Component lifecycle
  • State synchronization

When you know the underlying model, the API becomes intuitive instead of magical.

Top comments (6)

Collapse
 
himanshu_code profile image
Himanshu Sorathiya

Wow, really nice and beautifully explained.

Collapse
 
mohamad_msalme_38f2508ea2 profile image
Mohamad Msalme

Thank you for yur support

Collapse
 
sayanroy profile image
Sayan Roy

Thanks for that, great job explaining.

Collapse
 
mohamad_msalme_38f2508ea2 profile image
Mohamad Msalme

Thank you for your support

Collapse
 
parthupatel profile image
PARTH PATEL

Sounds really good.

Collapse
 
mohamad_msalme_38f2508ea2 profile image
Mohamad Msalme

Thanks