-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[TextareaAutosize] Improve implementation #40789
[TextareaAutosize] Improve implementation #40789
Conversation
Netlify deploy previewhttps://deploy-preview-40789--material-ui.netlify.app/ @material-ui/core: parsed: -0.06% 😍, gzip: -0.07% 😍 Bundle size reportDetails of bundle changes (Toolpad) |
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.
It seems to do the trick. Impressive!
}; | ||
} | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (renders.current === 20) { |
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.
I believe we still need a failsafe for unstable layouts (unrelated to React). This happens when the CSS values are unstable, this warning is also meant to invite developers to create issues so we can debug and fix the logic. Typically you would see the height flicker up and down as you type in the input. Or freeze the app because the resize observer logic run in a loop.
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.
for unstable layouts (unrelated to React). This happens when the CSS values are unstable,
Can you provide reproductions of layout instability not related to React? I'm open to debugging it.
this warning is also meant to invite developers to create issues so we can debug and fix the logic
Sure, but since we're not relying on React state anymore, there's no need for warnings about excessive re-renders caused by state.
Typically you would see the height flicker up and down as you type in the input. Or freeze the app because the resize observer logic run in a loop.
Again, can you provide reproductions using this PR build?
We need a regression test for #40557. |
@oliviertassinari Could you review further? |
I was blocked by this issue |
Olivier hasn't responded yet (since past two weeks), but since Michał has approved it, I think I can merge it. |
Fixes #40557
I was investigating #40557 and noticed related issues (#33081 and #20106). A React 18 compatibility issue was also addressed in #33253 using
ReactDOM.flushSync
. However, the problem persists in the latest version, as evident in #40557 and a PR attempt in #38453.Problem:
The current implementation sets a re-render limit of 20 in order to prevent infinite rendering errors caused by React. However, this limit also disrupts the synchronization of height beyond 20 re-renders, leading to incorrect layout and the warning described in #40557. The primary questions to address are: Why is the infinite re-render occurring, and what is the root cause?
Potential Cause of Too Many Re-renders / infinite loop:
The issue arises from using React state to store
height
andoverflow
, which are then passed to the inlinestyle
prop of the textarea. ThesyncHeight
function calculates the height, updates the state usingsetState
, triggering a re-render. The new re-render causes another call tosyncHeight
and a subsequent state update. Even if there are no layout changes or the constraint is not passed, the new reference of the object returned byprevState
causes repeated re-renders. Additionally, the absence of a dependency array in theuseEffect
forsyncHeight
triggers it on every component re-render, leading to an infinite loop.Solution:
It's correct that we have to call
syncHeight
on every render. This PR proposes avoiding the use of React state and instead directly setting the calculated HTML textarea'sheight
andoverflow
properties on the component DOM using a ref.Before and After for #40557 can be viewed here: (Make sure to also test by clicking on
Show/Hide
button)Closes #38453 (Stale PR)