-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Warning is changing a uncontrolled input of type radio to be controlled... #6779
Comments
It's a bug. 👍 I think it's reacting to |
I have a related issue, I'm getting a warning that I'm switching an uncontrolled component to a controlled one with this code: function CheckboxButton({checked, onChange, textComponent} = {}) {
return (
<label className='checkbox-button'>
<input
type='checkbox'
checked={checked}
onChange={onChange}/>
{textComponent}
</label>
);
} If I add a useless |
Yea, I saw this myself yesterday. |
This bug is a bit annoying. For one thing, there are many different input types to consider (https://www.w3.org/TR/html5/forms.html#attr-input-type) and browsers seem to support more types than the spec indicates (for instance, Mozilla appears to support "month" as per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input). In the particular case of a radio button, this is further complicated by the fact that you can't determine the "controlledness" of a group just by looking at a single input. You need to look at all the inputs with the same Too bad we can't just ban uncontrolled components. That would make life so much easier. :P. |
@jimfb Not sure if I'm following, as far as I understand it and is concerned it's just about checking whether
Mixing controlled and uncontrolled radios is a problem too, but an entirely separate problem IMHO. This warning is simply about switching an individual input between being controlled and uncontrolled. I.e.
I'm still of the opinion that they should be entirely separate components. |
im experiencing this also: clicking the radio shows the error
|
@syranide I think it's a bit more complicated than that. Think about how you would implement these warnings for switching between controlled and uncontrolled radio buttons. If any of the radio inputs within that form group have the |
Let's scope this to handling the 90% case which is just that this is warning incorrectly. We can worry about making it perfect some other time. |
@zpao That's what I'm talking about - this warning firing incorrectly (too often) for radio buttons. if we just want a quick fix, I think we need to just disable this warning for radio buttons. I don't think there is a 90% solution for radio buttons that won't occasionally fire spurious warnings. A "correct" fix will require tracking all the radio inputs within each form group. |
The test case in the initial report is warning and it should not be. It is not because of needing to track across radios in this case it's because we're incorrectly looking at You are talking about a real problem but it's tangential to this one. |
@zpao Right, but an uncontrolled radio group is not required to set a defaultChecked on any of the elements. And a controlled radio group is allowed to set a defaultChecked on a controlled element (for the reset button). This means that unless we do the smarter logic, any check we do is wrong and will fire spurious warnings. We might as well just remove the check (for radio buttons) at that point. |
@jimfb Huh? Isn't this how you use radio-buttons https://jsfiddle.net/dt03qw4p/? Just like Or what am I missing? |
@syranide Are you required to specify Example: this is a legal (immutable) controlled form:
Obviously it's a contrived example. You can imagine that the user has a list of elements and just "sets" the checked attribute on the one that is checked. But the result is the same. The "controlledness" of the input is defined by the entire group within the form. The lack of a That's why this warning is so tricky for radio buttons. |
@jimfb Actually the answer is yes in a sense...? https://jsfiddle.net/hx2r7bc4/ (contrived example, but still) Also, I would consider it very weird for someone to have a dynamic controlled component and not explicitly set
It works, I'm not sure if I would agree that it's a good idea. Regardless, that wouldn't be a problem, since checked is not provided a property to the others you can almost be certain that these are static radio buttons and not dynamic. So they would never change controlledness and warn anyway. Further, you should not use that pattern to create static radio buttons, that's what So again, this seems like a case of win-win to me, if you're doing it wrong/badly you get a warning. No one doing it right will get warning. Perhaps I've missed something, but I don't see any case involving good practice that would actually cause you to see the warning, only bad ones. |
I withdraw my earlier comment, the spec indicates that https://html.spec.whatwg.org/multipage/forms.html#checkbox-state-(type=checkbox) |
correction, MDN indicates that it's a required attribute: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox The spec doesn't actually seem to say that.. I'm not sure what the citation for MDN is but it seems incorrect to me. Personally I'd rather omit the |
Wow, that sucks. That is the most terrible thing I've seen all month. But I'll give you credit because it demonstrates a problem with not specifying the checked prop on a controlled component when all values are false.
I'm not sure it's that weird. It is pretty intuitive/natural to loop over some data and set Anyway, I suppose I'm fine with banning that behavior 👍. I think that will work. The only other edge case I'd worry about is what happens if a controlled checkbox turns into a controlled text input, but that should be easy for us to get right, we just need to test it after making our change(s). |
Just for posterity. We can't realistically get away from nullness of
IMHO, I think I had a PR about that that would consider it the same as remounting the component which I think everyone agreed but it never ended up merged. So until something like that happens I wouldn't even worry about it, it's virtually an error in a sense to change the type of an input today (there are other broken edge-cases too AFAIK). |
I have the same problem. This is my code:
And when I click the checkbox I got the same warning. |
It means |
Is this issue still be considered as bug? <input type="radio" name="a" defaultValue="1" defaultChecked/>
<input type="radio" name="a" defaultValue="11"/> |
@littlee I don't blame you, I blame us, but that's just terrible. |
@gaearon I think this should be explicitly declared with the examples in the Form docs , for example write a Note about this situation, or it would missunderstands the React beginers. |
@littlee, in short you mean Instead of |
I don't think it's related to what I said before |
i had same warning. casting value from so now i should put |
Per facebook/react#6779 React gets confused if the expression controlling the 'checked attribute on a checkbox is initially null (or, presumably, undefined). Force-casting to a Boolean removes the warning without changing the functionality.
try:
|
Yet another way is:
|
In my case, the I used the following code, the warning disappeared.
|
I have this code
https://jsfiddle.net/69z2wepo/42137/
And when I click on radio it gives me warning
Warning: Test is changing a uncontrolled input of type radio to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components
So its not clear why or is it a bug ?
The text was updated successfully, but these errors were encountered: