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

The getDerivedStateFromProps Mental Model

Understanding the old class lifecycle method helps recognize when effects are doing the same wrong thing.

The getDerivedStateFromProps Mental Model

You'll learn: How understanding an old React class method reveals why many useEffect patterns are fundamentally wrong.

The Old Class Component Pattern

In class components, React had a lifecycle method called getDerivedStateFromProps. It was designed to let components compute state values based on incoming props. Developers often misused it, creating the exact problem we see today with useEffect: trying to synchronize state with props when they should just compute values directly.

Here's the mental model:

// OLD CLASS ANTIPATTERN (don't do this)
static getDerivedStateFromProps(props, state) {
  if (props.userId !== state.prevUserId) {
    return {
      prevUserId: props.userId,
      userName: fetchUserName(props.userId) // ❌ Wrong!
    };
  }
  return null;
}

This became so problematic that the React team wrote extensive documentation explaining why you rarely need it. The solution? Compute derived values during render instead.

The Modern useEffect Parallel

When you write this:

// MODERN ANTIPATTERN (same mistake, different syntax)
useEffect(() => {
  setFilteredItems(items.filter(item => item.active));
}, [items]);

You're making the exact same mistake developers made with getDerivedStateFromProps. You're using a synchronization mechanism (effect) for something that should just be a calculation.

The class component experience taught the React team this lesson years ago. The guidance hasn't changed—only the syntax. If your effect is computing state from props or other state, you're in getDerivedStateFromProps territory, and you probably need a different approach.

Key Takeaway: The problems with getDerivedStateFromProps in class components are identical to many useEffect misuses today—both try to synchronize when you should just compute.