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

[TextareaAutosize] Fix warnings for too many renders in React 18 #33253

Merged
merged 3 commits into from Jun 23, 2022

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Jun 22, 2022

The issue is similar to the Masonry's flickering issue, solved in #33163 It is about using flushSync when doing state updates in resize observer.

Fixes #33081
Closes #32783

facebook/react#24331

Before (v5.8.6)

React 18 πŸ’₯

Codesandbox: https://codesandbox.io/s/multilinetextfields-demo-material-ui-forked-w47ngg?file=/package.json
*note that console warnings when updating the value in some of the input fields.

React 17 βœ…

Codesandbox: https://codesandbox.io/s/multilinetextfields-demo-material-ui-forked-bjxcy9?file=/package.json

After

React 18 βœ…

Codesandbox: https://codesandbox.io/s/multilinetextfields-demo-material-ui-forked-br9h2h?file=/demo.tsx

React 17 βœ…

Codesandbox: https://codesandbox.io/s/multilinetextfields-demo-material-ui-forked-etpepf?file=/package.json

@mui-bot
Copy link

mui-bot commented Jun 22, 2022

Details of bundle changes

Generated by 🚫 dangerJS against f7fba49

@mnajdova mnajdova marked this pull request as ready for review June 22, 2022 11:13
}, [maxRows, minRows, props.placeholder]);
}, [getUpdatedState]);

const syncHeightWithFlushSycn = () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically the same function as syncHeight, it is just using flushSync for updating the state. We cannot and shouldn't use flushSync in all other cases where we are synching the height, so we are using the same method as before.

@mnajdova mnajdova requested a review from a team June 22, 2022 12:01
@mnajdova mnajdova added bug πŸ› Something doesn't work component: TextareaAutosize The React component. labels Jun 22, 2022
return;
}

// In React 18, state updates in a ResizeObserver's callback are happening after the paint which causes flickering
Copy link
Member

@eps1lon eps1lon Jun 22, 2022

Choose a reason for hiding this comment

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

I know you're nerd-sniping me because I'm pretty sure this should be fixed with some experimental features πŸ˜› On vacation now but I'll definitely take a look later because having to reach for flushSync every time you're in a ResizeObserver callback seems odd and something you often miss unless you've encountered this bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha it's not intentional, but yes, I agree it's strange and should be fixed once someone is back from vacation 😁 Just kidding, but really looks like a strange requirement and more importanly is not easy to capture the bugs or create reliable tests.

@mnajdova mnajdova changed the title [TextareaAutosize] Fix issues related to React 18 [TextareaAutosize] Fix warnings for too many renders in React 18 Jun 23, 2022
@mnajdova mnajdova merged commit fd279d6 into mui:master Jun 23, 2022
@mnajdova mnajdova mentioned this pull request Jun 23, 2022
6 tasks
@eps1lon
Copy link
Member

eps1lon commented Aug 25, 2022

FYI for reproductions try to pin dependency versions. I wanted to revisit these repros but they all use latest.
Repro with pinned dependencies: https://codesandbox.io/s/multilinetextfields-demo-material-ui-forked-cq4bmf

@oliviertassinari
Copy link
Member

Test case added in #38728

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug πŸ› Something doesn't work component: TextareaAutosize The React component.
Projects
None yet
4 participants