material-ui: [TextField] `Too many re-renders. The layout is unstable.` error message with `multiline` enabled in controlled mode

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When using a multiline TextField in controlled mode the following error message in the console occurs multiple times:

MUI: Too many re-renders. The layout is unstable.
TextareaAutosize limits the number of renders to prevent an infinite loop.

Also, the text field is not correctly resized sometimes. When pressing a return sometimes no new line is added.

The error only happens in a controlled mode. The uncontrolled mode works fine.

Expected behavior 🤔

No error message. Correct resizing.

Steps to reproduce 🕹

Minimal StackBlitz sample: https://stackblitz.com/edit/react-ljalqg?file=demo.tsx

  1. Visit the above link
  2. Open and watch the StackBlitz console
  3. Focus the TextField and press enter several times

Context 🔦

No response

Your environment 🌎

`npx @mui/envinfo`
System:
    OS: Linux 5.16 Ubuntu 20.04.4 LTS (Focal Fossa)
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.15.0/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: Not Found
  npmPackages:
    @emotion/react: ^11.9.0 => 11.9.0 
    @emotion/styled: ^11.8.1 => 11.8.1 
    @mui/base:  5.0.0-alpha.83 
    @mui/icons-material: ^5.8.2 => 5.8.2 
    @mui/lab: ^5.0.0-alpha.84 => 5.0.0-alpha.84 
    @mui/material: ^5.8.2 => 5.8.2 
    @mui/private-theming:  5.8.0 
    @mui/styled-engine:  5.8.0 
    @mui/system:  5.8.2 
    @mui/types:  7.1.3 
    @mui/utils:  5.8.0 
    @mui/x-date-pickers:  5.0.0-alpha.1 
    @types/react: 18.0.10 => 18.0.10 
    react: 18.1.0 => 18.1.0 
    react-dom: 18.1.0 => 18.1.0 
    typescript: 4.7.2 => 4.7.2

Using Chrome.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

Not really worried about the error messages lol. More worried about the performance issues.

same problem… versions:

@mui/material”: “^5.13.7”, “react”: “^18.2.0”, I’m still encountering an error too

I’m currently facing an issue with the multiline functionality in the TextField component while using NodeJS 18.2 and MUI 5.13. Despite being on the latest versions, I’m still encountering an error. Whenever I try to use multiline, the TextField doesn’t behave as expected.

// mui-base/TextareaAutosize/TextareaAutosize.js
useEnhancedEffect(() => {
  syncHeight();
}, [value]); // added `value` as a depedency, instead of no depedencies at all

This issue always occurs for me (in a styled material TextField component , with some tailwind but nothing special) and the above code still fixes the issue in my case. Cannot really explain why it occurs for me and not for you guys. I ll try to investigate again soon

I just encountered this bug myself. My team mate could set minRows: {1} and the bug disappeared for him, but not for me on my computer.

We are working on the same branch in the same git repo. And we can’t figure out why this is happening. We also deleted all the node modules and re-installed.

The workaround for me was to add the rows prop e.g:

rows:"3"

but that fixates the textarea to only show 3 rows. If more rows are added it becomes a scrollable textarea. And that is not really what i wanted. So i don’t know if i really can call it a workaround but it works for now.

@mui/material": "^5.14.12

Do you and your teammate have different monitors? I had this happen when my textField was being constrained by css and couldn’t grow to target size.

Is there any sort of planned fix for this issue?

I’m still seeing it with the following versions:

"@mui/material": "^5.14.13",
"react": "^18.2.0",

I have checked the MUI docs and specifying rows should not be required, but if you dont set the row prop you get the bug.

I also tried setting minRows to 1 so the rest of the TextArea would resize automatically to fit the content but I got the same bug. If minRows is set to the exact number of rows you need + 1 you dont get the bug but if minRows is under or equal to the height of your TextArea you also get the problem.

The only solution that worked so far is setting the rows prop or setting the minRows prop to the number of rows of the text + 1, so I think it may be a problem of the autosizing calculation.

Is this the expexted behaviour of the component?? To require a rows input if multiline is true? This kind of defeats the purpose of auto-scaling up to the number of rows? I can confirm that if rows is not specified there is an infinite loop error. This is the version that I have: 5.14.11 Would this be looked into or this is the expected behaviour, just asking out of curiosity…

I encountered this error, and after some investigation, I realized that there is a bug in the multiline prop of the TextField. If you don’t specify the rows prop, it goes into an infinite loop and triggers the error. I fixed it by setting the rows prop to ‘5’, and the error stopped occurring.

This has been the solution that worked for me:

 <TextField
              label='Description'
              variant='filled'
              fullWidth
              rows='5'
              multiline
              sx={{ mb: 1 }}
              {...register('description', {
                required: 'This field is required',
                minLength: { value: 2, message: 'Minimum 2 characters' },
              })}
              error={!!errors.description}
              helperText={errors.description?.message}
            />

After this the infinite loop error was solved.