From 581c93db40ad9ac92d76a52fbf9274cf2a103232 Mon Sep 17 00:00:00 2001 From: Vitali Zaidman Date: Sat, 18 Jan 2025 16:29:00 +0000 Subject: [PATCH] updated readme --- README.md | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 31c8e37..a57be35 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,18 @@ `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.) -For example, if you pass `style={{width: '100%'}}` to a big pure component it would always re-render on every element creation: +For example, if you pass `style={{width: '100%'}}` to a big memo component it would always re-render on every element creation: ```jsx - + ``` - It can also help you to simply track when and why a certain component re-renders. +> **Notice**: The library was not tested at all with [React Compiler](https://react.dev/learn/react-compiler). I believe it's completely incompatible with it. + +> **Notice 2**: 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. + +> **Notice 3**: 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)! + ## Setup 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. * [For `React 18`, please see the readme for version @^8](https://github.com/welldone-software/why-did-you-render/tree/version-8). @@ -33,7 +38,7 @@ yarn add @welldone-software/why-did-you-render -D ``` Set the library to be the React's importSource and make sure `preset-react` is in `development` mode. -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). +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). ```js ['@babel/preset-react', { runtime: 'automatic', @@ -46,7 +51,7 @@ This is because React 19 requires using the `automatic` [JSX transformation](htt #### Bare workflow -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. +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. ```js module.exports = { @@ -54,7 +59,11 @@ module.exports = { env: { development: { - plugins: [['@babel/plugin-transform-react-jsx', { runtime: 'classic' }]], + plugins: [['@babel/plugin-transform-react-jsx', { + runtime: 'automatic', + development: process.env.NODE_ENV === 'development', + importSource: '@welldone-software/why-did-you-render', + }]], }, }, } @@ -81,7 +90,7 @@ module.exports = function (api) { }; ``` -> Notice: Create React App (CRA) ^4 **does use the `automatic` JSX transformation.** +> Notice: Create React App (CRA) ^4 **uses the `automatic` JSX transformation.** > [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) Create a `wdyr.js` file and import it as **the very first import** in your application. @@ -113,21 +122,18 @@ Import `wdyr` as the first import (even before `react-hot-loader` if you use it) import './wdyr'; // <--- first import import 'react-hot-loader'; -import {hot} from 'react-hot-loader/root'; import React from 'react'; import ReactDOM from 'react-dom'; // ... import {App} from './app'; // ... -const HotApp = hot(App); -// ... -ReactDOM.render(, document.getElementById('root')); +ReactDOM.render(, document.getElementById('root')); ``` -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. +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. -Otherwise, add `whyDidYouRender = true` to component classes/functions you want to track. (f.e `Component.whyDidYouRender = true`) +Otherwise, add `whyDidYouRender = true` to ad-hoc components to track them. (f.e `Component.whyDidYouRender = true`) More information about what is tracked can be found in [Tracking Components](#tracking-components).