Why did you render

Monkey patches React to notify you about avoidable re-renders.

README


Why Did You Render

npm version Build Status
NPM
Snyk Vulnerabilities for npm package Coverage Status

why-did-you-render by Welldone Software monkey patchesReact to notify you about potentially avoidable re-renders. (Works with React Native as well.)

For example, if you pass style={{width: '100%'}} to a big pure component it would always re-render on every element creation:
  1. ``` js
  2. <BigListPureComponent style={{width: '100%'}}/>
  3. ```

It can also help you to simply track when and why a certain component re-renders.

Setup

The library was tested (unit tests and E2E)) withReact@18, React@17 and React@16.

  1. ```
  2. npm install @welldone-software/why-did-you-render --save-dev
  3. ```
or
  1. ```
  2. yarn add --dev @welldone-software/why-did-you-render
  3. ```

If you use the automatic JSX transformation, set the library to be the import source, and make sure preset-react is in development mode.
  1. ``` js
  2. ['@babel/preset-react', {
  3.   runtime: 'automatic',
  4.   development: process.env.NODE_ENV === 'development',
  5.   importSource: '@welldone-software/why-did-you-render',
  6. }]
  7. ```

Notice: Create React App (CRA) ^4 does use the automatic JSX transformation.


Create a wdyr.js file and import it as the first import in your application.

wdyr.js:
  1. ``` js
  2. import React from 'react';

  3. if (process.env.NODE_ENV === 'development') {
  4.   const whyDidYouRender = require('@welldone-software/why-did-you-render');
  5.   whyDidYouRender(React, {
  6.     trackAllPureComponents: true,
  7.   });
  8. }
  9. ```

*Notice: The library should NEVER be used in production because it slows down React*


In Typescript, call the file wdyr.ts and add the following line to the top of the file to import the package's types:
  1. ```tsx
  2. ///
  3. ```

Import wdyr as the first import (even before react-hot-loader):

index.js:
  1. ``` js
  2. import './wdyr'; // <--- first import

  3. import 'react-hot-loader';
  4. import {hot} from 'react-hot-loader/root';

  5. import React from 'react';
  6. import ReactDOM from 'react-dom';
  7. // ...
  8. import {App} from './app';
  9. // ...
  10. const HotApp = hot(App);
  11. // ...
  12. ReactDOM.render(<HotApp/>, document.getElementById('root'));
  13. ```

If you use trackAllPureComponents like we suggest, all pure components (React.PureComponent or React.memo) will be tracked.

Otherwise, add whyDidYouRender = true to component classes/functions you want to track. (f.e Component.whyDidYouRender = true)

More information about what is tracked can be found in Tracking Components.

Can't see any WDYR logs? Check out the troubleshooting section or search in the issues.

Custom Hooks


Also, tracking custom hooks is possible by using trackExtraHooks. For example if you want to track useSelector from React Redux:

wdyr.js:
  1. ``` js
  2. import React from 'react';

  3. if (process.env.NODE_ENV === 'development') {
  4.   const whyDidYouRender = require('@welldone-software/why-did-you-render');
  5.   const ReactRedux = require('react-redux');
  6.   whyDidYouRender(React, {
  7.     trackAllPureComponents: true,
  8.     trackExtraHooks: [
  9.       [ReactRedux, 'useSelector']
  10.     ]
  11.   });
  12. }
  13. ```

Notice that there's currently a problem with rewriting exports of imported files in webpack. A quick workaround can help with it: #85 - trackExtraHooks cannot set property.


Read More

[Common fixing scenarios this library can helps with](http://bit.ly/wdyr02)
[React Hooks - Understand and fix hooks issues](http://bit.ly/wdyr3)
Why Did You Render v4 Released! - TypeScript support, Custom hooks tracking (like React-Redux’s useSelector), Tracking of all pure components.

Integration With Other Libraries


Sandbox

You can test the library in the official sandbox.


Tracking Components

You can track all pure components (React.PureComponent or React.memo) using thetrackAllPureComponents: true option.

You can also manually track any component you want by setting whyDidYouRender on them like this:
  1. ``` js
  2. class BigList extends React.Component {
  3.   static whyDidYouRender = true
  4.   render(){
  5.     return (
  6.       //some heavy render you want to ensure doesn't happen if its not necessary
  7.     )
  8.   }
  9. }
  10. ```

Or for functional components:

  1. ``` js
  2. const BigListPureComponent = props => (
  3.   <div>
  4.     //some heavy component you want to ensure doesn't happen if its not necessary
  5.   </div>
  6. )
  7. BigListPureComponent.whyDidYouRender = true
  8. ```

You can also pass an object to specify more advanced tracking settings:

  1. ``` js
  2. EnhancedMenu.whyDidYouRender = {
  3.   logOnDifferentValues: true,
  4.   customName: 'Menu'
  5. }
  6. ```

- logOnDifferentValues:

  Normally, only re-renders that are caused by equal values in props / state trigger notifications:
  1. ``` js
  2.   render(<Menu a={1}/>)
  3.   render(<Menu a={1}/>)
  4. ```
  This option will trigger notifications even if they occurred because of different props / state (Thus, because of "legit" re-renders):
  1. ``` js
  2.   render(<Menu a={1}/>)
  3.   render(<Menu a={2}/>)
  4. ```

- customName:

  Sometimes the name of the component can be missing or very inconvenient. For example:

  1. ``` js
  2.   withPropsOnChange(withPropsOnChange(withStateHandlers(withPropsOnChange(withState(withPropsOnChange(lifecycle(withPropsOnChange(withPropsOnChange(onlyUpdateForKeys(LoadNamespace(Connect(withState(withState(withPropsOnChange(lifecycle(withPropsOnChange(withHandlers(withHandlers(withHandlers(withHandlers(Connect(lifecycle(Menu)))))))))))))))))))))))
  3. ```
  

Options

Optionally you can pass in options as the second parameter. The following options are available:
- include: [RegExp, ...] (null by default)
- exclude: [RegExp, ...] (null by default)
- trackAllPureComponents: false
- trackHooks: true
- trackExtraHooks: []
- logOwnerReasons: true
- logOnDifferentValues: false
- hotReloadBufferMs: 500
- onlyLogs: false
- collapseGroups: false
- titleColor
- diffNameColor
- diffPathColor
- notifier: ({Component, displayName, hookName, prevProps, prevState, prevHook, nextProps, nextState, nextHook, reason, options, ownerDataMap}) => void
- getAdditionalOwnerData: (element) => {...}

include / exclude

(default: null)

You can include or exclude tracking of components by their displayName using the include and exclude options.

  1. ``` js
  2. whyDidYouRender(React, { include: [/^ConnectFunction/] });
  3. ```

Notice: exclude takes priority over both include and manually set whyDidYouRender =


trackAllPureComponents

(default: false)

You can track all pure components (both React.memo and React.PureComponent components)

Notice: You can exclude the tracking of any specific component with whyDidYouRender = false


trackHooks

(default: true)

You can turn off tracking of hooks changes.


trackExtraHooks

(default: [])

Track custom hooks:

  1. ``` js
  2. whyDidYouRender(React, {
  3.   trackExtraHooks: [
  4.     // notice that 'useSelector' is a named export
  5.     [ReactRedux, 'useSelector'],
  6.   ]
  7. });
  8. ```

There is currently a problem with rewriting exports of imported files in webpack. A workaround is available here: #85 - trackExtraHooks cannot set property


logOwnerReasons

(default: true)

One way of fixing re-render issues is preventing the component's owner from re-rendering.

This option is true by default and it lets you view the reasons why an owner component re-renders.

demo

logOnDifferentValues

(default: false)

Normally, you only want logs about component re-renders when they could have been avoided.

With this option, it is possible to track all re-renders.

For example:
  1. ``` js
  2. render(<BigListPureComponent a={1}/>)
  3. render(<BigListPureComponent a={2}/>)
  4. // will only log if you use {logOnDifferentValues: true}
  5. ```

hotReloadBufferMs

(default: 500)

Time in milliseconds to ignore updates after a hot reload is detected.

When a hot reload is detected, we ignore all updates for hotReloadBufferMs to not spam the console.

onlyLogs

(default: false)

If you don't want to use console.group to group logs you can print them as simple logs.

collapseGroups

(default: false)

Grouped logs can be collapsed.

titleColor / diffNameColor / diffPathColor

(default titleColor: '#058')
(default diffNameColor: 'blue')
(default diffPathColor: 'red')

Controls the colors used in the console notifications

notifier

(default: defaultNotifier that is exposed from the library)

You can create a custom notifier if the default one does not suite your needs.

getAdditionalOwnerData

(default: undefined)
You can provide a function that harvests additional data from the original react element. The object returned from this function will be added to the ownerDataMap which can be accessed later within your notifier function override.

Troubleshooting


No tracking

If you are in production, WDYR is probably disabled.
Maybe no component is tracked
    Check out Tracking Components once again.
    If you track all pure components (React.PureComponent or React.memo), maybe your none of your components are not pure.
Maybe you have no issues
    Try causing an issue by temporary rendering the whole app twice in it's entry point:
    
        index.js:
  1. ``` js
  2.         const HotApp = hot(App);
  3.         HotApp.whyDidYouRender = true;
  4.         ReactDOM.render(<HotApp/>, document.getElementById('root'));
  5.         ReactDOM.render(<HotApp/>, document.getElementById('root'));
  6. ```

Custom Hooks tracking (like useSelector)

There's currently a problem with rewriting exports of imported files in webpack. A quick workaround can help with it: #85 - trackExtraHooks cannot set property.

React-Redux connect HOC is spamming the console

Since connect hoists statics, if you add WDYR to the inner component, it is also added to the HOC component where complex hooks are running.

To fix this, add the whyDidYouRender = true static to a component after the connect:
  1. ``` js
  2.   const SimpleComponent = ({a}) => <div data-testid="foo">{a.b}</div>)
  3.   // not before the connect:
  4.   // SimpleComponent.whyDidYouRender = true
  5.   const ConnectedSimpleComponent = connect(
  6.     state => ({a: state.a})
  7.   )(SimpleComponent)
  8.   // after the connect:
  9.   SimpleComponent.whyDidYouRender = true
  10. ```

Sourcemaps

To see the library's sourcemaps use the source-map-loader.

Credit


Inspired by the following previous work:

https://github.com/maicki/why-did-you-update which I had the chance to maintain for some time.

https://github.com/garbles/why-did-you-update where A deep dive into React perf debugging is credited for the idea.

License


This library is MIT licensed.