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

When Derived State Depends on Props

Computing values from props during render instead of syncing props to state via useEffect.

When Derived State Depends on Props

What you'll learn: How to compute values from props during render without falling into the useEffect synchronization trap.

The Common Mistake

When you receive props and need to transform them, it's tempting to "sync" them into state using useEffect:

function UserGreeting({ firstName, lastName }) {
  const [fullName, setFullName] = useState('');

  // ❌ Don't do this!
  useEffect(() => {
    setFullName(`${firstName} ${lastName}`);
  }, [firstName, lastName]);

  return <h1>Hello, {fullName}!</h1>;
}

This creates two renders: one with stale state, then another after the effect runs.

The Right Approach: Compute During Render

Props are available immediately during render. Just calculate what you need:

function UserGreeting({ firstName, lastName }) {
  // ✅ Compute directly from props
  const fullName = `${firstName} ${lastName}`;

  return <h1>Hello, {fullName}!</h1>;
}

No effect. No extra state. No extra render. The derived value updates instantly whenever props change.

Think of It Like Math

If someone gives you two numbers and asks for the sum, you don't write it down on a sticky note (state), then calculate it later (effect). You just add them! Props are inputs; derived values are outputs. Calculate them in the same pass.

When Props Change

Every time firstName or lastName changes, the component re-renders with new props. Your calculation runs fresh with the new values automatically. That's exactly what you want.

Key Takeaway: When a value can be calculated from props, compute it during render—not in an effect. This eliminates unnecessary state, extra renders, and synchronization bugs.