Intermediate Reading #blog-posts #technical-articles #react #css

📰 Reading: Tech Articles & Blog Posts

3 exercises — read realistic developer blog posts about React performance and CSS layout. Follow technical arguments, extract key rules, and understand "rule of thumb" advice.

How developer blog posts are usually structured
  • Hook → common problem or surprising fact to grab attention
  • Explanation → the rule, concept, or mechanism
  • Pitfall / Gotcha → where developers go wrong
  • Fix / Solution → the recommended approach
  • "Key takeaway" / TL;DR → summary of the main point
0 / 3 completed
1 / 3
📰 React Performance Blog Post
Title: Why React Re-renders (And How to Stop Unnecessary Ones)

If you've been working with React for a while, you've probably noticed that components
sometimes re-render more often than you'd expect. Understanding why this happens is
crucial to building performant applications.

React re-renders a component when one of three things happens:
1. Its own state changes (via useState or useReducer)
2. Its parent component re-renders
3. A context it subscribes to changes (via useContext)

The third trigger surprises most developers. It means that even if none of your
component's props changed, it will still re-render if the parent does — unless you
wrap it in React.memo().

Here's a common pitfall:

  function ParentComponent() {
    const [count, setCount] = useState(0);
    const handleClick = () => setCount(c => c + 1);
    return (
      <>
        <button onClick={handleClick}>Increment</button>
        <ExpensiveChild onAction={handleClick} />
      </>
    );
  }

In this example, every time count changes, ParentComponent re-renders, which creates
a NEW handleClick function reference. Even if you wrap ExpensiveChild in React.memo(),
it will still re-render because the prop reference changed.

The fix: wrap handleClick in useCallback() so its reference remains stable across renders.

Key takeaway: React.memo() alone is not enough when passing callbacks as props.
You almost always need to pair it with useCallback() (and sometimes useMemo() for
computed values).
According to the article, why does wrapping a component in React.memo() NOT prevent re-renders in the code example shown?