-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Textarea does not respond to the value property changing to null #2533
Comments
I believe it's |
Here's the case I'm thinking of: Imagine a series of state transitions (perhaps instead of a single button the form would have some sort of selector that loads a series of objects from a data source). If one of those transitions sets a property to null, you end up with a value from the last object in the textarea. Doesn't seem right. I can see though what you're saying. I might be taking the concept of two way binding a little too literally - I'm currently working around it by doing something like this: http://jsfiddle.net/f1v1fac1/2/ ...which isn't a big deal. |
@syranide does setting to null make it uncontrolled? That doesn't sound right to me, but I haven't looked at the linkedstatemixin in a long time. I also would expect the behavior of the input here, not the textarea. |
@zpao http://jsfiddle.net/91khmysc/ Also yeah I agree, the more I think about it more it makes sense to "reset" it when becoming uncontrolled, it's the "most deterministic" one and also the one easiest to reason about. |
This one was driving me nuts too until I finally got down to the bottom of things. Here's a simpler example that doesn't use http://jsfiddle.net/wn7ymsa4/3/ Pretty sure if a |
The same problem also occurs for a |
I still think react should be checking for the presence of a
Removes any possible confusion since you no longer need to know all possible values of |
I don't disagree, but I think your line of thinking is a bigger change (that would also affect the documentation). So I'd suggest to go with the pull request #3041 for now. But for the overall picture, I think you're right. There should be a clearer distinction between controlled/uncontrolled behavior, that avoids edge cases like these: http://facebook.github.io/react/tips/controlled-input-null-value.html |
@rymohr While it makes sense in a way, it breaks with how all other props work and I feel like there's something inherently wrong about |
True, but this is about developer intention and "falling into the pit of success" as you guys like to say. It happens with Nobody passes a Switching to a key-presence check would simplify the underlying code and make controlled vs uncontrolled components easier to explain. It's easy to misinterpret the current documentation:
In the examples above, |
It should also be revealing that nobody's complained about the current behavior of |
cc @yungsters (for here and #3041) - I know it's been a while but you may remember some of your early thoughts around controlled components. I'm not sold that returning to |
@zpao Returning to To me it becomes quite obvious when you think of React as being inherently kind of stateless, for a certain set of props you should always get the same output. So rendering
Nothing else really makes sense to me in the context of React. |
But if the user typed something, we leave that value in the DOM on subsequent renders. You could think of React as stateless but the DOM isn't and we respect that for uncontrolled components.
If we treat changing the |
@zpao Yeah ofc. I improved my post a bit after I posted it. To me it comes down to the fact that transitioning from controlled to uncontrolled doesn't really make sense to me, you pick one and stick to it. You're not supposed to use controlled as a read-only state for your uncontrolled input. So if there's a controlled to uncontrolled transition, it's really a transition from EDIT: Or is there a use-case for transitioning from controlled to uncontrolled? Then it could make sense to keep the value, but I can't think of any... |
Also; it's easy to reproduce the behavior of the value sticking by simply updating |
@zpao Hmm, actually, let's rephrase it like this:
That was easy, now let's consider this instead:
I'm quite convinced now that the correct answer at step 6 is "bar" (i.e. it should reset to |
@zpao I agree that the approach should be consistent. The most consistent approach for
Imagine we have a controlled component with a non-null value that gets changed to
What is the reasoning behind the current definition/behavior? If there aren't any good reasons besides "that's just the way it is defined", then it would probably be better to change the definition. |
@martinstein You still have transitions between controlled and uncontrolled inputs regardless of whether EDIT: I think turning |
@syranide You are right, there are still transitions between those two types. But there would be fewer ambiguities/inconsistencies. Can you explain why you find And the 'rule-set' would just be simpler. Currently the rule is:
The simpler rule would be:
Generally, the simpler approach is the better and more intuitive one, right? |
@martinstein It's not so much that I find |
I don't think it breaks basic expectations. Most people would expect that From a language perspective, JavaScript specifically treats
That, for me is the expectation of how I agree that it's sometimes used as a short-cut to optimize code but in this case it makes the code more complicated (my pull-request would have been simpler without the special treatment of |
I think we've collectively beaten this one to death at this point. Any new eyes from the core team that can take a look at it and give a fresh take? |
I don't think that setting As for whether or not We should fix |
@yungsters But if we take my example above (copied below), I think the only thing that make sense is to "get the latest value and set it before becoming uncontrolled" which is equivalent to a reset. The example from above, consider this scenario:
That's obvious, now let's consider this instead where the new state is not rendered due to batching (or w/e):
I would argue quite strongly that the value should still be "bar", whether or not the component actually performed intermediate re-renders should be irrelevant, especially as it's affected by the batching strategy. If we reset when becoming uncontrolled then it's always consistent. That said however, I don't think going from controlled to uncontrolled is something you should do (the best solution would be if it wasn't at all possible if you ask me), we should simply make the best of it. But if you do, this is the only one that makes sense to me in the context of how React is meant to work, this is deterministic in a way that "keeping the last value" simply isn't. |
@jimfb Expanding on that, I would go as far as saying it's either a mistake or bad practice. First; if controlled/uncontrolled where separate components (as they really should be IMHO), it would be impossible to switch like this. Second; it's trivial to implement uncontrolled behavior using a controlled input, so if what you really want is some non-standard behavior you're best off implementing your own component on-top of controlled inputs that implements whatever behavior you want, not switching between uncontrolled/controlled. My point being; yes, there may be valid use-cases, but anyone being serious about their code can and should always avoid the warning as above. So I personally don't see any negative sides to having this warning. |
value || '' is not a good solution because sometimes you have number 0 which is controlled too and you will lose this value 0 || '' => '' here is acceptable fix: function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
return '';
}
return value;
} |
Caused by #589 fix and fix #586 via facebook/react#2533 (comment)
Caused by #589 fix and fix #586 via facebook/react#2533 (comment)
Hi everyone, Nevertheless, is this the final call? Or are we waiting for a confirmation this is the target implementation? (just to know if we will have to change our code anytime soon) Cheers |
@iboxgithub You should not rely on this behavior and I believe 0.15 will issue a warning if you do. |
You mean 15.0
|
Thanks for your feedback --> so what is THE best practice :) |
@iboxgithub An input should be either uncontrolled (value always undef/null) or controlled (value is a string, so it should be an empty string rather than null) for its entire lifetime. |
I don't think there's anything actionable here, so I'm going to close this out. We still warn when trying to reset a controlled input using
|
When the value property of a textarea changes to null or undefined from a non blank value, the text displayed in the DOM does not change. Here is a fiddle that captures the issue - notice that behavior is different for the text input.
http://jsfiddle.net/f1v1fac1/
The text was updated successfully, but these errors were encountered: