-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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: Broken effect in useCSSTextTruncation hook #22324
Conversation
Codecov Report
@@ Coverage Diff @@
## master #22324 +/- ##
==========================================
+ Coverage 66.85% 66.86% +0.01%
==========================================
Files 1847 1847
Lines 70482 70481 -1
Branches 7721 7720 -1
==========================================
+ Hits 47120 47127 +7
+ Misses 21362 21360 -2
+ Partials 2000 1994 -6
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
/testenv up |
@geido Ephemeral environment spinning up at http://34.213.128.129:8080. Credentials are |
/testenv up FEATURE_HORIZONTAL_FILTER_BAR=true |
@geido Ephemeral environment spinning up at http://54.187.101.39:8080. Credentials are |
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.
LGMT!
Ephemeral environment shutdown and build artifacts deleted. |
(cherry picked from commit 2731cba)
SUMMARY
As discovered as part of #22317, the
useCSSTextTruncation
hook had a difficult to track down bug: the effect responsible for updating theisTruncated
value based on DOM measurements wouldn't run, even though console logs showed that values in its dependency array were changing.Upon investigation, it seems this is an intended behavior of React, as described by this comment: even if dependency arrays change, React will abort the entire render cycle (including effects) if neither props, state nor context have changed. Because the dependency array of the effect in question is currently based on values derived from refs, changes to these values don't indicate to React that the render cycle is "worth it", so if no other props, state or context changes in the component calling the hook, the render cycle is thrown out and the effect that would update the
isTruncated
value never runs.To fix this, this PR removes the use of refs to store intermediate values and instead adds a new effect that runs every render and sets the
offsetWidth
andscrollWidth
state variables to whatever the current DOM measurement is. No dependency array or checks for changes are necessary to avoid infinite render cycles because updating state in functional components to the same value does not trigger a re-render. This PR then updates the previously-existing effect to be much simpler, taking advantage of this same no-op behavior of setting the state to an existing value.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Example that's broken in #22317:
Screen.Recording.2022-12-02.at.8.24.32.PM.mov
#22317 after cherry-picking this commit:
Screen.Recording.2022-12-02.at.8.25.21.PM.mov
TESTING INSTRUCTIONS
Check that truncation-based tooltips still work as expected wherever
useCSSTextTruncation
is used:DateFilterLabel
in Dashboard native filters (vertical + horizontal) and Explore ad-hoc filtersADDITIONAL INFORMATION
HORIZONTAL_FILTER_BAR