Skip to content

Commit

Permalink
[TextareaAutosize] Fix crash when used with React 18 & Suspense (mui#…
Browse files Browse the repository at this point in the history
  • Loading branch information
howlettt authored and Daniel Rabe committed Nov 29, 2022
1 parent 6edc26a commit 21f7c76
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/mui-base/src/TextareaAutosize/TextareaAutosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(props, ref)
// In React 18, state updates in a ResizeObserver's callback are happening after the paint which causes flickering
// when doing some visual updates in it. Using flushSync ensures that the dom will be painted after the states updates happen
// Related issue - https://github.com/facebook/react/issues/24331
// TODO: Do this only in the resize observer?
flushSync(() => {
setState((prevState) => {
return updateState(prevState, newState);
Expand All @@ -156,7 +155,16 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(props, ref)
React.useEffect(() => {
const handleResize = debounce(() => {
renders.current = 0;
syncHeightWithFlushSycn();

// If the TextareaAutosize component is replaced by Suspense with a fallback, the last
// ResizeObserver's handler that runs because of the change in the layout is trying to
// access a dom node that is no longer there (as the fallback component is being shown instead).
// See https://github.com/mui/material-ui/issues/32640
// TODO: Add tests that will ensure the component is not failing when
// replaced by Suspense with a fallback, once React is updated to version 18
if (inputRef.current) {
syncHeightWithFlushSycn();
}
});
const containerWindow = ownerWindow(inputRef.current);
containerWindow.addEventListener('resize', handleResize);
Expand Down

0 comments on commit 21f7c76

Please sign in to comment.