You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-21Lines changed: 39 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,25 +12,36 @@
12
12
13
13
`why-did-you-render` by [Welldone Software](https://welldone.software/) monkey patches **`React`** to notify you about potentially avoidable re-renders. (Works with **`React Native`** as well.)
14
14
15
-
For example, if you pass `style={{width: '100%'}}` to a big pure component it would always re-render on every element creation:
15
+
For example, if you pass `style={{width: '100%'}}` to a big memo component it would always re-render on every element creation:
16
16
```jsx
17
-
<BigListPureComponent style={{width:'100%'}}/>
17
+
<MemoBigList style={{width:'100%'}}/>
18
18
```
19
-
20
19
It can also help you to simply track when and why a certain component re-renders.
21
20
21
+
> [!CAUTION]
22
+
> The library was not tested with [React Compiler](https://react.dev/learn/react-compiler) at all. I believe it's completely incompatible with it.
23
+
24
+
> [!CAUTION]
25
+
> Not all re-renders are *"bad"*. Sometimes shenanigan to reduce re-renders can either hurt your App's performance or have a neglagable effect, in which case it would be just a waste of your efforts, and complicate your code. Try to focus on heavier components when optimizing and use the [React profiler](https://legacy.reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html) inside the React dev-tools to measure the effects of any changes.
26
+
27
+
> [!NOTE]
28
+
I've joined the React team, specifically working on React tooling. This role has opened up exciting opportunities to enhance the developer experience for React users— and your input could offer valuable insights to help me with this effort. Please join the conversation in the [discussion thread](https://github.com/welldone-software/why-did-you-render/discussions/309)!
29
+
22
30
## Setup
23
-
The latest version of the library was tested [(unit tests and E2E)]((https://travis-ci.com/welldone-software/why-did-you-render.svg?branch=master)) with **`React@18`** only. For React 17 and 16, please use version @^7.
31
+
The latest version of the library was tested [(unit tests and E2E)]((https://travis-ci.com/welldone-software/why-did-you-render.svg?branch=master)) with **`React@19`** only.
32
+
*[For `React 18`, please see the readme for version @^8](https://github.com/welldone-software/why-did-you-render/tree/version-8).
33
+
*[For `React 17` and `React 16`, please see the readme for version @^7](https://github.com/welldone-software/why-did-you-render/tree/version-7).
Set the library to be the React's importSource and make sure `preset-react` is in `development` mode.
32
43
33
-
If you use the `automatic` JSX transformation, set the library to be the import source, and make sure `preset-react` is in `development` mode.
44
+
This is because `React 19` requires using the `automatic`[JSX transformation](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html).
34
45
```js
35
46
['@babel/preset-react', {
36
47
runtime:'automatic',
@@ -43,15 +54,19 @@ If you use the `automatic` JSX transformation, set the library to be the import
43
54
44
55
#### Bare workflow
45
56
46
-
Unfortunately, the `metro-react-native-babel-preset` that comes with react-native out of the box does not allow you to change the options of the `babel/plugin-transform-react-jsx` plugin. Just add the plugin with options as listed below and start react-native packager as usual. Default env for babel is "development". If you do not use expo when working with react-native, the following method will help you.
57
+
Add the plugin as listed below and start react-native packager as usual. Default env for babel is "development". If you do not use expo when working with react-native, the following method will help you.
> [See the following comment on how to do this step with CRA](https://github.com/welldone-software/why-did-you-render/issues/154#issuecomment-773905769)
83
98
84
-
Create a `wdyr.js` file and import it as **the first import** in your application.
99
+
Create a `wdyr.js` file and import it as **the very first import** in your application.
85
100
86
101
`wdyr.js`:
87
102
```jsx
@@ -95,35 +110,36 @@ if (process.env.NODE_ENV === 'development') {
95
110
}
96
111
```
97
112
98
-
> **Notice: The library should *NEVER* be used in production because it slows down React**
113
+
> [!CAUTION]
114
+
> The library should *NEVER* be used in production because:
115
+
> - It significantly slows down React
116
+
> - It monkey patches React and can result in unexpected behavior
99
117
100
118
In [Typescript](https://github.com/welldone-software/why-did-you-render/issues/161), call the file wdyr.ts and add the following line to the top of the file to import the package's types:
If you use `trackAllPureComponents` like we suggest, all pure components ([React.PureComponent](https://reactjs.org/docs/react-api.html#reactpurecomponent) or [React.memo](https://reactjs.org/docs/react-api.html#reactmemo)) will be tracked.
140
+
If you use `trackAllPureComponents`, all pure components ([React.PureComponent](https://reactjs.org/docs/react-api.html#reactpurecomponent) or [React.memo](https://reactjs.org/docs/react-api.html#reactmemo)) will be tracked.
125
141
126
-
Otherwise, add `whyDidYouRender = true` to component classes/functions you want to track. (f.e `Component.whyDidYouRender = true`)
142
+
Otherwise, add `whyDidYouRender = true` to ad-hoc components to track them. (f.e `Component.whyDidYouRender = true`)
127
143
128
144
More information about what is tracked can be found in [Tracking Components](#tracking-components).
129
145
@@ -241,7 +257,8 @@ Optionally you can pass in `options` as the second parameter. The following opti
> There is currently a problem with rewriting exports of imported files in webpack. A workaround is available here: [#85 - trackExtraHooks cannot set property](https://github.com/welldone-software/why-did-you-render/issues/85)
303
+
> This feature is rewriting exports of imported files. There is currently a problem with that approach in webpack. A workaround is available here: [#85 - trackExtraHooks cannot set property](https://github.com/welldone-software/why-did-you-render/issues/85)
287
304
288
305
#### logOwnerReasons
289
306
##### (default: `true`)
@@ -325,10 +342,11 @@ If you don't want to use `console.group` to group logs you can print them as sim
0 commit comments