Installing and Configuring why-did-you-render
You'll learn how to install and properly configure why-did-you-render in your development environment to automatically detect unnecessary re-renders.
Why Install This Library?
You've learned to manually track unnecessary renders using DevTools and console.log, but why-did-you-render automates this detective work. Think of it as installing a security camera that automatically alerts you when something suspicious happens—except it's watching your component renders.
Installation
First, install the library as a dev dependency:
npm install @welldone-software/why-did-you-render --save-dev
Basic Configuration
Create a new file in your project root (or src folder) called wdyr.js:
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');
whyDidYouRender(React, {
trackAllPureComponents: false,
trackHooks: true,
logOnDifferentValues: true,
});
}
Then import this file before your main App component in index.js:
import './wdyr'; // Must be first!
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
Key Configuration Options
trackAllPureComponents: Set totrueto automatically monitor all memoized components (can be noisy)trackHooks: Detects unnecessary re-renders caused by hookslogOnDifferentValues: Shows what props/state actually changed
Enabling Per-Component
To track specific components, add this property:
MyComponent.whyDidYouRender = true;
Key Takeaway: Install why-did-you-render as a dev dependency, configure it before your app loads with environment checks, and selectively enable tracking to automatically catch unnecessary renders without manual DevTools inspection.