-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
onChange not firing on controlled input element when value
is updated
#8971
Comments
value
is updated
It actually works for me, every time I change the input field I see the 'CHANGE' text inside console. you can check my pen http://codepen.io/eduardb/pen/zNmmRN with fix |
Hi @eduardbcom, yeah of that I know. I guess my example wasn't 100% clear. What you should try doing is to click the button and watch for the 'CHANGE' inside the console. You don't get one when you are updating the I'll update my codepen so this is more clear by adding the functionality you thought was missing. EDIT: I've now updated the Codepen to make it more clear that the problem is when when you are trying to update the |
Thanks for the report @pudgereyem, the example was really useful! This is expected behavior. The Check out this example that uses plain JavaScript: var input = document.getElementById('input')
// This is only called when the input value
// is changed via the DOM, not when its changed
// programmatically using input.value
input.onchange = (e) => {
window.alert(e.target.value)
}
setTimeout(() => {
input.value = "changed"
}, 1000) No change event is fired when
If the value is coming from |
I can just add that you can use refs to emit change event by hand. |
@aweary Thanks alot for the fast response.
Got it! Yes, it's an interesting discussion I think. I understand that it's now only meant to be a standard event handler. However, the input did change, so it would be neat if @eduardbcom yes I know, but thanks for adding that! |
Hi @aweary and @eduardbcom, Here is a new pen with a solution to my problem where I manually trigger Achieved using |
I've found what to me appears as a bug.
Bug
When a controlled
input
element changes by updating it's value,onChange
isn't called.Reproduction
I made a codepen that illustrates the problem; http://codepen.io/pudgereyem/live/bgmvOq
Expected behaviour
I expect that
onChange
gets called, since the<input/>
did change, and the world of React is by nature very much "controlled".This is written in the docs and also mentioned in various threads such as #8550 (comment) and #8696 (comment).
My problem
The fact that the inputs
onChange
function not gets called is a problem to me, because I need to do logic (such as validation) when the inputs have changed.Comments
onInput
also, but it doesn't get called eitherThe text was updated successfully, but these errors were encountered: