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

Avoiding useState + useEffect for Prop Mirroring

Why copying props into state then syncing them in effects creates bugs and how to avoid this pattern.

Avoiding useState + useEffect for Prop Mirroring

What you'll learn: Why copying props into local state and syncing them with useEffect creates bugs and unnecessary complexity.

The Antipattern

A common mistake is "mirroring" props into local state, then using useEffect to keep them in sync:

function UserProfile({ userId }) {
  const [id, setId] = useState(userId);

  useEffect(() => {
    setId(userId);  // ❌ Prop mirroring antipattern
  }, [userId]);

  // ... use id instead of userId
}

This seems logical: "When the prop changes, update my state." But it creates serious problems.

Why It's Broken

1. Stale state on first render: Your component renders with the old id value before the effect runs and triggers a re-render with the new value.

2. Unnecessary re-renders: You force React to render twice every time the prop changes—once with stale state, then again after the effect updates it.

3. Confusing bugs: If the prop updates multiple times quickly, your state can fall out of sync, leading to UI showing the wrong data.

The Fix: Just Use the Prop

function UserProfile({ userId }) {
  // ✅ Just use the prop directly
  return <div>User: {userId}</div>;
}

Props are already reactive. When userId changes, your component re-renders automatically with the new value. No state needed. No effect needed.

The Analogy

Mirroring props is like receiving mail at your door, then immediately copying each letter into a notebook, then reading from the notebook instead of the letter. Why add the extra step? Just read the letter directly.

Key Takeaway: Never copy props into state just to use them—props are already perfectly reactive. Use them directly in your render logic. State is for values your component controls, not values passed from above.