-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[devtools] Prevent incorrect render detection for user components in didFiberRender (#33423) #33434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[devtools] Prevent incorrect render detection for user components in didFiberRender (#33423) #33434
Conversation
Improve rendering detection accuracy by adding actual input change verification for user components that have PerformedWork flag set. This prevents showing "The parent component rendered" message and highlight updates for components that didn't actually re-render due to bailouts. - Add props/state/ref comparison for user components after PerformedWork check - Restore original props comparison logic for host components - Fixes issue where bailout components were incorrectly marked as rendered
I tested your solution and it seems to work. Adding this to the top of the function instead seems to achieve the same thing. Not sure if they are practically equivalent or not
|
Thanks for testing it out! You're right — So while it's a great optimization to add on top, it can't fully replace the deeper equality check. Open to thoughts on this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for upstreaming this, but that's not the right fix for this problem.
Please see my comment here for more context.
@hoxyq I read your comment there and understood it, but I don't see how that alone rules out this change. Perhaps you just know from experience and the intent of this method that it's not right. Can you explain? The issue when I debugged this seems to be that didFiberRender returns true for fibers that didn't render in the current pass, but potentially rendered a long time ago. In the error case, it's called with the same fiber instance in both arguments, which is why I suggested simple equality #33434 (comment) That also appeared to fix it in my testing without any of the changes in this PR being necessary Are you saying the function is correct, but it should never be called for these fibers? |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! I'm a grad student working on a research project about using large language models to automate code review. Based on your commit 995f9da and the changes in packages/react-devtools-shared/src/backend/fiber/renderer.js, my tool generated this comment:
- Null Checks: Ensure that
nextFiber
is validated before accessing its properties to avoid potential null reference exceptions. - Behavioral Change: The logic change from returning a boolean based on
PerformedWork
to a more complex condition may affect existing components that rely on the previous behavior. Ensure that any components using this function are tested to confirm they handle the new logic correctly. - Testing Required: Given the changes in logic, thorough testing is required to ensure that existing functionality is not broken. Pay special attention to components that may not have been updated to handle the new checks.
- Flag Check Logic: The new logic correctly identifies that if no work was performed (
PerformedWork
flag is not set), it should not report a render. - Return Type Consistency: Ensure that the function's return type is consistent and well-documented, especially since it now has multiple return points.
- Comparison of Fiber Properties: The additional checks comparing
prevFiber
andnextFiber
properties (i.e.,memoizedProps
,memoizedState
, andref
) are a necessary optimization to prevent unnecessary renders. - Combine Conditions: Extract the logic that checks multiple properties of
prevFiber
andnextFiber
into a separate function (e.g.,areFibersEqual(prevFiber, nextFiber)
) to improve readability. - Early Return Optimization: Consider optimizing the order of checks. If
getFiberFlags(nextFiber)
is expensive, checkprevFiber
properties first, especially if they are often equal. This could reduce calls togetFiberFlags
. - Separation of Concerns: The logic for determining whether to report a render is tightly coupled within the
attach
function. It may be beneficial to extract this logic into a separate utility function. - Magic Numbers: The use of the
PerformedWork
constant as a magic number could be abstracted further. Consider defining a more descriptive constant or an enum that explains its purpose. - Documentation Update: If this function is part of a public API, consider updating the documentation to reflect the new behavior and any version dependencies to avoid confusion for users relying on the previous implementation.
As part of my research, I'm trying to understand how useful these comments are in real-world development. If you have a moment, I'd be super grateful if you could quickly reply to these two yes/no questions:
- Does this comment provide suggestions from a dimension you hadn’t considered?
-
- Do you find this comment helpful?
Thanks a lot for your time and feedback! And sorry again if this message is a bother.
Summary
Fixes false positive rendering detection in React DevTools Profiler by improving the
didFiberRender
function to accurately detect when user components actually re-render, preventing misleading "The parent component rendered" messages.Problem
Previously, React DevTools would incorrectly mark components as "rendered" even when they didn't actually re-render due to bailouts. This happened because the
didFiberRender
function only checked thePerformedWork
flag, but React can set this flag even during bailout scenarios.Example scenario:
Solution
Enhanced
didFiberRender
function for user components (ClassComponent, FunctionComponent, etc.):This change ensures that:
PerformedWork
flag (performance optimization)Testing
Test Setup:
Used the following test case with independent Count and Greeting components:
Test Results:
✅ Tested and verified with this code
// Before
Screen.Recording.2025-06-04.at.13.17.03.mov
// After
Screen.Recording.2025-06-04.at.13.17.35.mov
Before Fix:
After Fix:
Related
This change specifically targets user components (Function/Class components) and maintains existing behavior for host components, ensuring accurate rendering detection across the React component tree.
Fixes #33423 , #19732