In React, what’s the difference between onChange and onInput?

It seems there is no real difference

React, for some reason, attaches listeners for Component.onChange to the DOM element.oninput event. See the note in the docs on forms:

React docs – Forms

There are more people that are surprised by this behavior. For more details, refer to this issue on the React issue tracker:

Document how React’s onChange relates to onInput #3964

Quote from the comments on that issue:

I don’t understand why React chose to make onChange behave like onInput does. As fas as I can tell, we have no way of getting the old onChange behaviour back. Docs claim it’s a “misnomer” but not it isn’t really, it does fire when there’s a change, just not until the input also loses focus.

For validation, sometimes we don’t want to show validation errors until they’re done typing. Or maybe we just don’t want a re-render on every keystroke. Now the only way to do that is with onBlur but now we also need to check that the value has changed manually.

It’s not that big of a deal, but it seems to me like React threw away a useful event and deviated from standard behaviour when there was already an event that does this.

I agree 100% with the comment… But I guess changing it now would bring more problems than it solves since so much code had already been written that relies on this behavior.

React is not part of the official Web API collection

Even though React is built on top of JS, and has seen a huge adoption rate, as a technology React exists to hide a whole lot of functionality under its own (fairly small) API. Once area where this is obvious is in the event system, where there’s a lot going on under the surface that’s actually radically different from the standard DOM event system. Not just in terms of which events do what, but also in terms of when data is allowed to persist at what stage of the event handling. You can read more about that here:

React Event System

Leave a Comment