Skip to content

[TextareaAutosize] prevent redundant state updates#32783

Closed
Yama-Tomo wants to merge 1 commit intomui:masterfrom
Yama-Tomo:fix-redundant-state-updates
Closed

[TextareaAutosize] prevent redundant state updates#32783
Yama-Tomo wants to merge 1 commit intomui:masterfrom
Yama-Tomo:fix-redundant-state-updates

Conversation

@Yama-Tomo
Copy link

fixes: #20106

Fixed an infinite loop caused by the interaction of useEnhancedEffect(useLayoutEffect) and useState by skipping unnecessary state updates.

I have confirmed that this patch works in the following reproduction codes.
#20106 (comment)

NOTE: Applying this change may make it unnecessary to limit the number of renderings in the future 😎
#19743

@mui-bot
Copy link

mui-bot commented May 16, 2022

Details of bundle changes

Generated by 🚫 dangerJS against 99cef6d

@oliviertassinari oliviertassinari added the PR: needs test The pull request needs tests. label May 16, 2022
@Yama-Tomo
Copy link
Author

Yama-Tomo commented May 17, 2022

@oliviertassinari To reproduce the infinite loop, useEnhancedEffect(useLayoutEffect) would have to be emulated on unit tests(node.js), which would increase the complexity of the test.
Also, the test that counts the number of calls setState, which is the main topic of this changes, knows too many implementation details and is not an ideal test.
I'm just reducing the number of calls setState, so I'm thinking that passing the existing test is sufficient. what test is needed?

@oliviertassinari
Copy link
Member

oliviertassinari commented May 17, 2022

I personally don't understand how the changes make things different from the current state of things (we already have a piece of logic to prune rerenders), nor what problem we are trying to solve, please expand on both.

By "needs test" the minimum required is a way to reproduce the bug. Note that we never merge PRs to fix a bug we didn't reproduce. Also note that our tests infrastructure support browser tests, both running end-to-end and in the browser runtime (preferred).

@Yama-Tomo
Copy link
Author

By "needs test" the minimum required is a way to reproduce the bug.

As I mentioned in the PR description, the bug is reproducible. #20106 (comment)

@SeanMengis

This comment was marked as resolved.

@aeharding
Copy link

Found this bug in the wild and it led me here.

This can be easily repro'd on official MUI documentation for the "controlled" multiline textarea when you open in codesandbox.

Repro

  1. Visit https://mui.com/material-ui/react-text-field/#multiline
  2. Click "Edit in CodeSandbox"
  3. Press <enter> to create a new line in the top left textbox.

image

I would really appreciate a fix getting merged. Thanks.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 1, 2022

@aeharding Great thanks, so it seems to be React 18.1.0 specific. It works correctly on React 17.0.2, proof: #20106 (comment). To figure out what's the root problem. The current diff in the PR makes me think that we are not solving the root problem. Meaning that as soon as we hit a case where the layout is slightly unstable, we would face the same infinite rendering/height out-of-sync problem (the warning only being the tip of the iceberg).

@oliviertassinari oliviertassinari added scope: text field Changes related to the text field. type: regression A bug, but worse, it used to behave as expected. type: bug It doesn't behave as expected. labels Jun 1, 2022
@retrixe retrixe mentioned this pull request Jun 5, 2022
6 tasks
@InumberX
Copy link

InumberX commented Jun 14, 2022

@oliviertassinari @Yama-Tomo

#32783 (comment)

I still don't know why I get this error in React v18, but it may be a problem with syncHeight() using useEnhancedEffect (useLayoutEffect?) in the following
I changed this to React.useEffect instead of useEnhancedEffect and the error no longer occurs.
Is there any inconvenience in using useEffect?

useEnhancedEffect(() => {
  syncHeight();
});

React.useEffect(() => {
  syncHeight();
});

This message is written using the translation function.
Sorry if it is difficult to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: TextareaAutosize The React component. type: regression A bug, but worse, it used to behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Material-UI: too many re-renders. The layout is unstable.

7 participants