Why Did You Render logo

Why Did You Render

A monkey-patching utility for React that alerts you in the console about potentially avoidable component re-renders.

npm install why-did-you-render
12.3K1.2K/weekv1.0.13.67 KBMIT39 issues
Last updated: 2020-05-17
Star history chart for welldone-software/why-did-you-render

TL;DR

A debugging tool that monkey-patches React to detect when a component re-renders despite its props remaining effectively unchanged.

Essential for performance tuning, it helps identify redundant render cycles caused by broken memoization or referential instability.

Why Why Did You Render?

React's render cycle is fast, but unnecessary re-renders are the silent killer of application performance. Identifying exactly why a pure component re-rendered—was it a new object reference? An anonymous function? A prop value change?—is notoriously difficult with standard DevTools. why-did-you-render (often abbreviated as WDYR) automates this investigation.

  • Instant Feedback: It monkey-patches React to intercept render lifecycles and logs a detailed diff to the console whenever a component re-renders needlessly.
  • Reference Check: Specifically highlights the classic "same value, different reference" problem (e.g., passing { style: {} } or () => {} as props), which breaks React.memo.
  • Deep Equality Detection: It warns you if a re-render occurred even though the props are deeply equal, signaling a missed opportunity for memoization.
  • Granular Control: You can track all pure components globally or opt-in specific components individually to reduce noise.
  • Hook Tracking: Beyond components, it can also track custom hooks to see if they are triggering updates redundantly.

Code Snippet

To use WDYR, you initialize it at the very top of your application entry point, ensuring it only runs in development mode.

// wdyr.js
import React from 'react';

if (process.env.NODE_ENV === 'development') {
  const whyDidYouRender = require('@welldone-software/why-did-you-render');
  whyDidYouRender(React, {
    trackAllPureComponents: true, // Log all pure components automatically
  });
}

// In your component file
const MyComponent = React.memo((props) => {
  return <div>{props.count}</div>;
});

// Or opt-in manually if trackAllPureComponents is false
MyComponent.whyDidYouRender = true; 

export default MyComponent;

When MyComponent re-renders unnecessarily (e.g., receiving a new object reference that looks identical to the old one), WDYR prints a colorful diff in the console explaining the cause.

Pros and Cons

No library is perfect; understanding the trade-offs is key to selecting the right tool.

Pros

  • Performance Education: It not only finds bugs but teaches developers about referential equality and how React's reconciliation works.
  • Time Saver: Eliminates hours of manual "commenting out code" to isolate the source of a re-render loop.
  • High Visibility: The console output is explicit, showing the "prev" and "next" values of the props that triggered the update.

Cons

  • Monkey Patching: It modifies React's internal prototype, which can occasionally conflict with other libraries or future React versions (though usually stable).
  • Console Noise: In large applications, turning it on globally (trackAllPureComponents) can flood the console, making it hard to read. It's best used selectively.
  • Development Only: It significantly adds overhead and must be strictly excluded from production bundles to avoid slowing down the app for users.

Comparison with Other Profiling Tools

The table below outlines the positioning differences between WDYR and other performance tools:

LibraryDesign PhilosophyBest ForPain Points
Why Did You RenderActive Alerting
Proactively shouts at you via the console when a mistake is detected during runtime.
Fixing Re-renders
Pinpointing specific referential equality issues in React.memo or hooks.
Setup & Noise
Requires correct initialization order; can be spammy if not configured well.
React DevTools (Profiler)Passive Recording
Records a session and presents a flamegraph for post-execution analysis.
Overview Analysis
Seeing the entire component tree's render cost and identifying the most expensive branches.
Manual Investigation
Tells you that a component rendered, but often requires digging to understand why (e.g., "hooks changed").
React ScanVisual Highlighting
Draws outlines on the screen around components as they render in real-time.
Visual Feedback
Quickly spotting "flashing" areas of the UI that shouldn't be updating.
Lack of Detail
Shows activity but doesn't provide the deep data diff explaining the root cause (prop vs state).

Verdict: When to Adopt

why-did-you-render is a specialized diagnostic tool, not a permanent fixture. Adopt it temporarily when you notice sluggish performance or during a dedicated "performance sprint." It is the single best tool for solving "I wrapped it in React.memo but it's still re-rendering" mysteries. Once the issues are resolved, you can disable it to keep your development console clean.