Masterbar: Do not recreate masterbar button element on every render #98897
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposed Changes
In this PR we move the
MenuItem
wrapper fromMasterbarItem
to a standalone component so React can recognize it and keep its references stable.Fixes #98893
Why are these changes being made?
This fixes a bug introduced by #98510. That PR introduced a new React wrapper called
MenuItem
to wrap the button or anchor element rendered byMasterbarItem
. However, that wrapper was created inline (viaconst MenuItem = ...
) inside the render method of its parent. As a result, React re-creates it on every render. This is mostly invisible to the user but it means that the underlying HTML element is being recreated very frequently.One consequence of this is that any references to the element will quickly go out of date.
The
Popover
component uses one such reference to determine its position on the page. When the reference points to aMasterbarItem
, the reference goes out of date. By the time it calculates the position, it is no longer pointing to an element on the page and gets a position of all 0s.Testing Instructions
/plans
and add a product to your cart./home
.