Skip to content
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

fix(dropdown): stabilize internal references to reduce re-renders #17952

Conversation

tay1orjones
Copy link
Member

@tay1orjones tay1orjones commented Nov 1, 2024

Closes #17897

This makes Dropdown's internal references (variables, functions, props, etc) more stable to reduce re-renders by ~98% 🤯

Changelog

Changed

  • Move stateReducer to be delcared outside of the component
  • Wrap onSelectedItemChange in useCallback
  • Memoize the readOnlyEventHandlers, and wrap the onKeyDownHandler it uses with useCallback
  • Memoize the normalizedSlug that clones the slug prop and forces the mini size
  • Prevent unnecessary state updates by updating stateReducer by returning state directly instead of a new object every time (this alone was a huge improvement)
  • Memoize items
  • Memoize selectProps and all the props passed in (isItemDisabled,onHighlightedIndexChange)
  • Memoize toggleButtonProps
  • Add enableFloatingStyles to the dependency array to ensure menuProps will get updated

Testing / Reviewing

  • Ensure all stories for Dropdown work as expected, including keyboard interaction, cursor interaction, and selection
Click to expand test results

The benchmark I tested against is the original bug repro with the following interactions, resulting in 4674 renders.

2024-11-01.at.13.45.43-MAIN-Google.Chrome.mp4

The first commit reduces this same interaction to 2145 renders.

2024-11-01.at.14.11.22-MAIN-Google.Chrome.mp4

The second commit is even better, down to just 89 renders.

2024-11-01.at.14.51.19-MAIN-Google.Chrome.mp4

The first two counts are somewhat of an incorrect count because they're tied to mousemove interaction. The update to stateReducer is what pulled that down so significantly.

Copy link

netlify bot commented Nov 1, 2024

Deploy Preview for v11-carbon-web-components ready!

Name Link
🔨 Latest commit 386ed78
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-web-components/deploys/672bc60f71ca91000859ebdb
😎 Deploy Preview https://deploy-preview-17952--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 1, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 386ed78
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/672bc60ff01ffe000811da93
😎 Deploy Preview https://deploy-preview-17952--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Nov 1, 2024

Deploy Preview for v11-carbon-react ready!

Name Link
🔨 Latest commit 386ed78
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/672bc60f1b10db00078c8d88
😎 Deploy Preview https://deploy-preview-17952--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Nov 1, 2024

Codecov Report

Attention: Patch coverage is 98.27586% with 1 line in your changes missing coverage. Please review.

Project coverage is 81.82%. Comparing base (7f7d88a) to head (386ed78).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ackages/react/src/components/Dropdown/Dropdown.tsx 98.27% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17952      +/-   ##
==========================================
+ Coverage   81.81%   81.82%   +0.01%     
==========================================
  Files         406      406              
  Lines       14053    14070      +17     
  Branches     4405     4409       +4     
==========================================
+ Hits        11497    11513      +16     
  Misses       2394     2394              
- Partials      162      163       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tay1orjones tay1orjones marked this pull request as ready for review November 1, 2024 20:04
@tay1orjones tay1orjones requested a review from a team as a code owner November 1, 2024 20:04
Copy link
Member

@emyarod emyarod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me!

@tay1orjones
Copy link
Member Author

I'll need to remove the test story before this can merge

@tay1orjones tay1orjones requested review from alisonjoseph and removed request for annawen1 November 6, 2024 17:40
Copy link
Member

@alisonjoseph alisonjoseph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 LGTM!

@tay1orjones tay1orjones added this pull request to the merge queue Nov 6, 2024
@tay1orjones tay1orjones removed this pull request from the merge queue due to a manual request Nov 6, 2024
@tay1orjones tay1orjones enabled auto-merge November 6, 2024 19:40
@tay1orjones tay1orjones added this pull request to the merge queue Nov 6, 2024
Merged via the queue into carbon-design-system:main with commit 4b77a2a Nov 6, 2024
40 checks passed
@tay1orjones tay1orjones deleted the 17897-dropdown-render-optimizations branch November 6, 2024 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: [Dropdown] Uncaught Error: Maximum update depth exceeded.
3 participants