-
-
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
[TextField] focused={true} and disabled={true} causes an infinite render #24777
Comments
Thanks for the report.
I definitely agree. We should just remove this (and similar props). State should live in a single place (context, props, actual state). Mixing it, makes it hard to reason about and bloats the implementation needlessly. I'm adding this to #24450 which already tracks this problem space. |
@ameem91 It does look difficult, indeed. I can't find any information in the error that could allow a developer to figure out the origin without going into a long trial & error exploration. I think that we can look into making it easier to spot. Looking at the source, it seems that the infinite loop comes from a wrong branching logic. I believe it should have been: diff --git a/packages/material-ui/src/FormControl/FormControl.js b/packages/material-ui/src/FormControl/FormControl.js
index e0da18ccfc..b5575867bd 100644
--- a/packages/material-ui/src/FormControl/FormControl.js
+++ b/packages/material-ui/src/FormControl/FormControl.js
@@ -161,7 +161,7 @@ const FormControl = React.forwardRef(function FormControl(inProps, ref) {
const [focusedState, setFocused] = React.useState(false);
const focused = visuallyFocused !== undefined ? visuallyFocused : focusedState;
- if (disabled && focused) {
+ if (disabled && focusedState) {
setFocused(false);
} So disabled wins over focused and only resets what it's allowed to. On a larger note raised by Sebastian. The We could add a warning when focused and disabled are both set from the outside. I would personally not spend time on it, but why not. |
Then the TextField is not focused and should still not allow the Edit: Closing since it is still tracked as part of #24450 |
In this case, I would propose we rename the prop
It's also less confusing as it's not about the DOM focus, nor the aria focus. @ameem91 What's your use case for the prop? |
Thanks for reporting this @ameem91, I was pulling my hair off. I know this is released, but I am still on an old build. |
If you render a TextField with the
focused
anddisabled
flags both set to true, it causes an infinite render.Current Behavior 😯
Code:
Error:
Expected Behavior 🤔
While it probably doesn't make sense to set both
focused
anddisabled
at the same time, I believe that the current error (too many re-renders) is not correct behavior. It's breaks the app and is difficult to debug. Instead, we can default to one offocused
ordisabled
if both are set and add a console warning.Steps to Reproduce 🕹
https://codesandbox.io/s/material-ui-issue-forked-kd970?file=/src/Demo.js
The text was updated successfully, but these errors were encountered: