-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Inaccurate warning when value props is set without onChange. #1118
Comments
@darthapo Two-way binding ( PS. Also, it's just a helpful warning, it's quite (OCD-)annoying that they can't be turned off though... |
On a related note, I'm wondering... When making a custom control that needs to properly support var node= this.refs.control.getDOMNode(),
fakeEvent= { target:node }
React.addons.TestUtils.Simulate.change( node, fakeEvent ) Or if there's no actual input element to propagate as the target, maybe: var node= this.getDOMNode(),
fakeEvent= { target:{ name:this.props.name, value:"NEWVALUE" } }
React.addons.TestUtils.Simulate.change( node, fakeEvent ) |
Another problem with the same warning: Is there a specific reason not to allow onKeyUp and onKeyDown instead of onChange? I have value depending on the state of the component and want to intercept keyCodes to change it. Also I want a blinking cursor so readOnly is not an option. The workaround is to use empty function for onChange but would be nice to avoid it. |
@Evernight It's only a helpful warning, it is not wrong not to have |
@Evernight Can you post a simple example of what your code looks like? I wasn't under the impression that listening to keydown/keyup will work with controlled components. (By the way, how would you want to react if someone were to paste in text or move it around using the mouse, etc., in a way that doesn't cause key events?) |
@syranide Yeah, but it's nicer to have no warnings (especially at runtime) when there probably shouldn't be any. @spicyj Here: http://jsfiddle.net/cf76B/5/ . It behaves exactly as I want it to, I am not exactly sure why it ignores the mouse events though. |
@Evernight What do you mean, it ignores the mouse events? This is a strange case and you can write |
@spicyj I mean I can't move text around or paste/delete the text using mouse. Probably some debugging would explain it but the current behavior suits my needs. Maybe the case is not typical indeed, I still can use empty function as a workaround. |
@Evernight Yes, this is because you're not handling onChange. It sounds like you might want an uncontrolled component. See http://facebook.github.io/react/docs/forms.html. |
This seems like a "wontfix" as it's only meant as a helpful warning, not an error. |
How about extending support of implementing UPDATE: FWIW the most significant usage of Actually disregard this Sorry, the onChange normalization you've done mitigates most of my usage of onInput from some legacy code. |
I came across this issue today as well. This is a bit weird because all I want is to set initial input values based on this.state, so there seems no particular reason for me to provide onChange in such case. Is this going to be fixed? Or, does it mean I'm doing anything improper? |
@ktei you might want to consider using |
@tirdadc thanks for the advice. That works. |
I'm getting this warning with the following example:
Is this warning helping by indicating I'm doing something wrong? Surely this is a perfectly acceptable design pattern? |
Stumbled into this same issue, where I am using event bubbling to handle lots of controlled input change events from one parent (the form element). While I understand that it's just a helpful warning, it is triggered a huge number of times for big forms, so I used the following hack and PROBABLY A VERY EXTREMELY BAD IDEA to suppress it:
Make sure you put that in a file named something like Any ideas for how this could potentially be supported more officially, or if that's something worth doing? To avoid the mess of detecting if and how the controlled inputs are being handled correctly, maybe there can be a way to declare in your component that you understand the warning and don't need it? |
@syranide Unfortunately, it seems that React sometimes goes a little bit overboard with the "helpful warnings". This is one of those cases. |
This warning is very often helpful for beginners. Those who know that they really don't want an |
…`onChange` handler. …” Good discussion here: facebook/react#1118
I was having this problem too due to passing a function as a property to a component and binding that function to an input field's onChange value.
|
This is extremely annoying. Having single onChange handler on form and using bubbling is a common pattern, specially for big forms. And having console full of warnings because of this is very strange. Basically, React is throwing warning at you when you're using something from "best practice" book. |
This is my opinion, but I'd say it's the opposite, it's a bad idea. It relies on bubbling outside of the React hierarchy. It's not portable and it's not something you find in other UI frameworks and not something you could reimplement yourself. Again, entirely my opinoin, but I even wish React would prevent this from being possible, it's a source of a pain and goes against the spirit of React. |
https://facebook.github.io/react/docs/forms.html#interactive-props
|
Having the same issue. Example:
|
Any solution? I have the same issue |
+1 |
Same issue here . in my case value comes from autopopulted username & email fields
|
What about adding: |
it works ! |
Just wanted to leave another use-case where this can happen. I'm using a custom CSS checkbox where the a click handler needs to be on the text and the checkbox.
The state is correctly stored on the input, but the user-agent style for checkboxes is set to |
I came across a different use case for the same issue. I have a checkbox wrapped in a div and decided to change the state of the checkbox with onClick event instead of onChange because onClick event of the div element will be executed before onChange event. My component looks something like this:
In the above example, doSomethingElse will be executed first, then i will stop propagating the event. However, if I used onChange instead of onClick in the checkbox element, doSomething will be executed before doSomethingElse, and in this case I will not be able to stop propagating the event. Here is an example My point is: if onClick is provided in checkbox input, there should not be a warning |
Yeah i have the same issue and i think that we should have a way to turn this warning of, or remove it, because it is not an "helpful" message but an error represented on the console. |
This will also raise this warning... when a checkboxes (or any input really) does not control its own checked/value attribute.... you might have any arbitrary element that controls it. <button aria-controls="checkbox" onClick={() => this.setState({checked: !this.state.checked})}>{'Check it!'}</button>
<input id="checkbox" type="checkbox" checked={this.state.checked} /> While I can see this being valuable for newbies - these edge cases just add noise to the logs in other cases.... that said, it would be neigh impossible to come up with a propType for this that was accurate / useful 100% of the time. Also, fwiw - the checked warning for checkboxes only shows if the value of checked is |
@tswaters In your case the checkbox should be disabled or readOnly. |
This is a particularly annoying warning message, especially in my case, as I am passing an onChange handler and everything looks and works perfectly in development but when I try testing my form component the tests are riddled with these warnings which I'm pretty sure are masking the crux of the problem I'm trying to solve. Whats more adding |
+1 on this issue. I had to attach a bogus If I provide a |
I had the same problem.... Instead value=, use the defaultValue |
If the only reason you don’t want to put In other cases, if you really want to “break out” of React’s model, you can use uncontrolled components as an escape hatch. They won’t show this warning. If you have a use case for which neither of those options works well, please file a new issue and describe it in more detail. |
I have the same sort of issue, where I'm handling value changes in onKeyDown. I can fix it by adding a no-op onChange handler, as mentioned above, but that seems absurd just to work around this (inaccurate) warning. I'm not sure how an uncontrolled component would really work in this case, either; I mean, I guess I could add a handler using vanilla JS on the ref element, but that sounds very dangerous. |
Uncontrolled components limit you to the basic HTML form elements. As soon as you need to implement slider / picker on top of DIV and custom markup – you need to make it controlled. In my case I really want to "break out" of React's model (I use CycleJS-like architecture with declarative stream-based events) and I'd really wish to disable that warning. |
@scabbiaza I don't believe this is true. Can you clarify what you mean? |
Uncontrolled component has its state controlled by Browser, stored in DOM. You can't make an "uncontrolled div" because browser applies no default behavior to divs.
it's not always possible or desired. Uncontrolled components are limited to built-in UI elements. And with controlled components, bubbling and other totally fine architectural patterns which React "doesn't favor" are screwed by this warning. I believe warning should either be 100% to the point, otherwise it's better be removed. I personally see a disturbing trend to add more and more warnings to React, in parallel with more and more requests of options to disable them. This particular warning is false in many cases, belongs to documentation area and should be removed i.m.o. |
Why don't convert this warning to a yellow notice, since not having onChange on every input is valid when you rely on event bubbling. |
How about using What are the pitfalls? Will the performance drain be noticeable for a large form? |
Here's a reproducable example: https://codesandbox.io/s/xrnl20np7w |
You still have to attach Whereas... <form onChange={e => {
e.preventDefault()
changeFormValues({
...oldFormValues,
[e.target.id]: e.target.value
})
}}>
<input type="text" id="foo" />
</form> ...can/should work just fine without this error. I understand the desire to keep warnings in for newbs (although no one will get far in React unless they grok the whole controlled input thing...), but Either way, I run into this semi-regularly and don't want to put Here it is throwing errors with the trending Easy Peasy library: https://codesandbox.io/s/easy-peasy-playground-with-react-warning-e2wug At the very least could we add something to ESLint or something to disable this warning? |
@gaearon @sophiebits I know you're busy, but friendly ping just to put this back on your radar in hopes you'll reconsider removing this warning or at least giving us a way to opt out of it. I think a lot of people would really like to be able to do <input onChange />
<input onChange />
<input onChange /> Thank you for all your hard work. |
6 years late but essentially this! <input
type="url"
value={value}
onChange={suppressMissingOnChangeHandlerWarning}
onBlur={e => onChange(e.currentTarget.value)}
/> Works great! In case it is unclear, function suppressMissingOnChangeHandlerWarning() {} |
When you create an input component and set its
value
without providing anonChange
handler, React will log this to the console:This isn't entirely accurate however. For example in my app I rely on event bubbling so that I can set a single
onChange
on the form component, therebysaving my sanitypreventing me from having to add anonChange
to every. single. field. (There are a lot in the app I'm working on)The text was updated successfully, but these errors were encountered: