welldone-software / why-did-you-render

why-did-you-render by Welldone Software monkey patches React to notify you about potentially avoidable re-renders. (Works with React Native as well.)
https://www.npmjs.com/package/@welldone-software/why-did-you-render
MIT License
11.2k stars 201 forks source link

WDYR does not show reason for render #263

Closed karpo518 closed 1 year ago

karpo518 commented 1 year ago

This problem is most likely not related to your plugin, but perhaps you can help with this. I have written a small application with a gantt chart. https://codesandbox.io/s/github/karpo518/react-gantt-chart

I used a special pattern in it that prevents all consumers from rendering when the context changes. The problem is that when the data for one consumer changes, the previous consumer is render, that is, the consumer that uses the part of the context that was changed earlier. If you run my application in a sandbox and try to collapse the "Custom issue level #4" task, the text "Render TaskContainer 4" will be displayed in the console. This is normal behavior. Clear the console and collapse the "Marketing Launch" task. You will see in the console that not only TaskContainer 1 was rendered, but also TaskContainer 4. This is rather strange. The same reference to the object is passed to the setState of the Task Container 4 component, and the object itself has not changed since the previous render. Your plugin also doesn't show the reason for the render, but the render is happening. Why is this happening?

vzaidman commented 1 year ago

It's a very peculiar issue. I've spent a lot of time debugging it.

When I was about to report it, I found out this bug is already reported to the React team. See https://github.com/facebook/react/issues/18098

Anyway, this is the solution to the issue: image

karpo518 commented 1 year ago

Wow. The cause of the problem turned out to be more complicated than I thought. But your solution turned out to be simple enough for me to use it and forget about unexpected renders. It's so simple that I'm even ashamed that I didn't think of it myself. Thank you for your time. I highly appreciate your work. Thanks to you, I know both the cause of the problem and its solution. Thanks again!