Tracking Components with whyDidYouRender = true
You'll learn how to mark specific React components for performance tracking using the whyDidYouRender static property.
Why Mark Components for Tracking?
By default, why-did-you-render only tracks components you explicitly opt into. This prevents console noise from thousands of components and lets you focus on the ones you suspect are causing performance issues. Think of it like putting a GPS tracker only on packages you're worried might get lost—not every single box in the warehouse.
Adding the Static Property
To track a function component, add a static property called whyDidYouRender and set it to true:
function ExpensiveList({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
// Mark this component for tracking
ExpensiveList.whyDidYouRender = true;
Now when ExpensiveList re-renders unnecessarily (same props but renders anyway), you'll see detailed console warnings explaining why. The tool will show you which props changed, whether they changed by value or reference, and whether the render was avoidable.
Selective Monitoring
You don't need to track every component—start with the ones appearing frequently in your Profiler flamegraph or those you've already identified as hot spots using the React DevTools. Add the flag, interact with your app, and read the console output. Remove the flag once you've diagnosed and fixed the issue.
Key Takeaway: Add ComponentName.whyDidYouRender = true as a static property to any function component you want to monitor for unnecessary re-renders—this gives you detailed console diagnostics for that specific component without flooding your logs.