-
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
Treat value={null} as empty string #11417
Comments
@sophiebits Is this interpretation correct? Was there any reason we didn't do this in 16? |
I don't remember. I don't think it's obvious which is better: I could imagine a case where you expect |
Not to scope creep this thread, but wouldn't you generally use value for controlled inputs and defaultValue for uncontrolled? I guess I don't see why someone would use both attributes on the same element. |
Allowing (Aside: it would also be nice to be able to bind Date objects to the value of |
I face this issue often when building controlled input's who rely on a record from state slice (such as an Immutable.Record via redux). I skip using something like defaultValue, because as my input changes, I dispatch various actions to update the record (which other components listen to, etc for various selections, calcs, etc) and ultimately the controlled value is 100% dependent on the value prop passed itself. I usually just set in my record the default value as '', but it would be great to use null since I can tell (without a pristine flag) if the field is pristine or simply been edited based on this concept. As noted here, defaultValue only satisfies the initial life cycle & muddying the two seems like an extraneous step: |
This problem happens to me very often when I'm using I have to change initial values to either I'd like so much to be able to use const someObject = {
a: null,
};
someObject.a; // existent property, defined as null
someObject.b; // inexistent property, returns undefined
/* versus */
const someObject = {
a: undefined,
};
someObject.a; // existent property, defined as undefined
someObject.b; // inexistent property, but returns undefined as above |
I'm new to React and still learning the ropes. It surprised me that controlled components do not allow nulls. Coming from a SQL background To work-around this shortcoming in React I've created shadow property flags for any fields that can be null -- with logic at commit time to use null for 'empty' fields that are marked in my shadow properties as nullable. The point is that I shouldn't have to do this. |
Running into this myself. Similar scenarios as described above (API returns object with null property values). I'm looking to go with the solution @RyanNerd is proposing, but it doesn't feel right. Alternatively, I can change my input to To avoid breaking existing projects, would it be possible perhaps to have an attribute that allowed you to specify that the input is controlled, regardless of the value?
I'm new to React and know very little about its internals. Just throwing something out there. |
I understand @sophiebits concern with how changing React to suddenly start treating |
Another case where this is really annoying is when using Material UI's The time wasted on petty little issues like this can really add up. Sometimes I find it more annoying than major bugs. I like @ernestopye's suggestion, I would just say Personally, I wish that that the uncontrolled case were opt-in only via an |
My takeaway so far is that changing the semantics of If we do it, I think other people will come with complaints — just as valid as the ones in this thread — about It's understandably frustrating. I agree that the core of this issue is that |
I don't use uncontrolled inputs so I'm struggling to see why this can't be changed. In a comment above, @sophiebits mentioned that someone might use @gaearon What other scenarios do you think will cause people to complain if this gets changed? Do you have use cases that would break that you see in the Facebook code-base somewhere? |
@gaearon is correct when he said:
The core of this problem isn't with React. It's with the insane JavaScript language itself:
Given that JavaScript is demonstrably insane and inconsistent with how nulls are treated what are we to do? This appears to me to be a matter of type safety. With controlled components React would need to be told what type to expect if the |
Has this issue been implemented? I recently discovered that in React 16.3+ a value of Examples demonstrating the change: I have a concern with how this impacts checkbox type inputs. When a checkbox does not have a value attribute set, the value should be set to |
@ericvaladas Your sandbox examples appear to be using Last comment from @gaearon on this issue was:
So it surprises me if React has updated how nulls or undefined are handled. |
There is a difference between undefined and null. React MUST allow null entry (which literally means in every database system that it will be hooked up to "no entry") on form fields. This is well understood in JSON as well. undefined should be used to mean uncontrolled. Null is controlled and should not be confused with empty strings either. The 2 are very different. An empty string means an entry with no characters. Null means NO ENTRY which are VERY different from eachother both in signaling intent AND in storage. This is VERY clear in Typescript where I would typically define an optional text field as:
This would require that it isn't undefined. This would be undefined or string:
While that is nicer for typescript it may not be desirable in React, but pick either undefined or null as uncontrolled and stick with it so that we know one way or another. I would guess at this point to not break anything that undefined has to equal uncontrolled and explicit NULL set would still be controlled and mean NO ENTRY. Microsoft for DECADES had this very same problem where Date Pickers in VB 6 and then .NET WinForms wouldn't accept null so you were FORCED to have an entry value because somehow someone in Microsoft couldn't figure out that "No Entry" is entirely valid on a field. It took them until WPF to get their act together and do it right. |
See @gaearon's last comment: #11417 (comment). At this time it's unlikely we'll see any changes to this because the new behavior is not clearly better. I'm going to lock this issue; please file a new one if you are having a problem you can't solve using the information already in this thread or file an RFC with a detailed motivation and explanation if you have a concrete proposal about how to change the behavior. |
Per @gaearon's request, I'm opening up a new issue based on #5013 (comment).
Currently, if you create an input like
<input value={null} onChange={this.handleChange} />
, the null value is a flag for React to treat this as an uncontrolled input, and a console warning is generated. However, this is often a valid condition. For example, when creating a new object (initialized w/ default values from the server then passed to the component as props) in a form that requires address, Address Line 2 is often optional. As such, passing null as value to this controlled component is a very reasonable thing to do.One can do a workaround, i.e.
<input value={foo || ''} onChange={this.handleChange} />
, but this is an error-prone approach and quite awkward.Per issue referenced above, the React team has planned on treating null as an empty string, but that hasn't yet occurred. I'd like to propose tackling this problem in the near future.
Please let me know if I can help further.
The text was updated successfully, but these errors were encountered: