-
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
[RFC] onChange -> onInput, and don't polyfill onInput for uncontrolled components #9657
Comments
Could you link the src code of the polyfilling?
I think this is great,we can just change this,why to change the api?
What does the "one version" mean?maybe a typo?
If have to do this,please just in DEV mode. And about the Uncontrolled Components,if you add the onInput for it,maybe we should add something in the doc if ref prop be set too,because if we have onInput,there is no necessity to use ref in most cases. |
I believe he means propagated as in 'a lot of people use it in higher level components'
For the next version of react, it'll work as it currently does, but with a deprecation warning. |
@deecewan Thanks for your share.
Thanks,I misunderstand it.
The next sentence he uses the word "next version" but this sentence he uses "one version",that's why I confuse. |
@sebmarkbage I've previously brought up separating uncontrolled and controlled inputs entirely and you guys seemed to generally agree, might this be a good time for that too or is that no longer relevant? I.e. Regarding |
@syranide I think |
@sebmarkbage I'm not entirely sure what you mean but it sounds like you've thought about it 👍 On topic, perhaps it would be an idea to settle for a unique event name, say Perhaps it's not a big deal, but having access to the polyfilled event for uncontrolled inputs can probably come in handy too sometimes. |
@trueadm Proposed the same thing (a new name).
I've been thinking about it for a bit. Right now we have very complex polyfills for all our events but really there's only a few things that we polyfill and override. E.g. our |
@sebmarkbage if browser vendors are going to make There are a LOT of people using Can we warn in 15.x that we're going to change |
I really the general concept of this change. It allows simplifying a really complicated polyfill while also actively encouraging folks to use the better controlled input pattern. I'd really encourage ya'll to please give a full major version for this to warn, and not add it in v15.6. The entire "form" library ecosystem in React is built on top of the behavior of onChange, and custom inputs most copy the onChange/value pattern. |
IMO losing access to |
Another reason is access to |
The spec has a very clear example where both of these events make sense.
Maybe https://codesandbox.io/s/j7074wxxvv illustrates the difference between current |
I think in the long-term we probably want to move away from adding more event namespaces to ReactDOM, including the changing of |
@trueadm I don't think I understand what you mean by DOM namespace or event namespace. At least not in the context of I'm not even sure if namespaced events are part of the current spec. |
@eps1lon Sorry for the confusion. What I meant by namespace, was that they conflict with the already existing event names (a property on the |
Wondering if you guys going to proceed in this |
It seems to me that, instead of waiting for this issue to be resolved, you can simply write a custom input component, which exposes the DOM-based import { Component, createElement, InputHTMLAttributes } from 'react';
export interface CustomInputProps {
onChange?: (event: Event) => void;
onInput?: (event: Event) => void;
}
/**
* This custom input component restores the 'onChange' and 'onInput' behavior of JavaScript.
*/
export class CustomInput extends Component<Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'onInput' | 'ref'> & CustomInputProps> {
private readonly registerCallbacks = (element: HTMLInputElement | null) => {
if (element) {
element.onchange = this.props.onChange ? this.props.onChange : null;
element.oninput = this.props.onInput ? this.props.onInput : null;
}
};
public render() {
return <input ref={this.registerCallbacks} {...this.props} onChange={undefined} onInput={undefined} />;
}
} Please let me know if you see ways to improve this approach or encounter problems with it. I'm still gaining experience with this |
Any update on this issue? |
Bump |
onChange
is a nicer name for whatonInput
does and the fact that it has propagated up to other high-level components as the default name is much nicer thanonInput
as a high level event name.Generally it has been helpful for the many new-comers to React that don't know the DOM well (which is a lot more than the inverse). However, that doesn't change the fact that it can be confusing for people that are familiar.
Unfortunately, changing it now would cause confusion for everyone that already knows React.
The reason I'd like to change it now is because I'd like to get away from polyfilling it for uncontrolled components. This use case is filled with all kinds of imperative code which leads to edge cases. E.g. reading/setting
e.target.value
or reading/settingref.value
.When you use controlled components you shouldn't need to touch them imperatively and therefore won't hit the edge cases. Ideally we should get away from reading from
e.target.value
and instead just pass thevalue
directly to the event handler.Proposal:
Controlled Components
onInput
: Polyfilled and works likeonChange
does today. It is allowed to over-fire many events even if nothing changed. May have special Fiber rules regarding synchronous flushing. Optional: Passvalue
as second arg.onChange
: Works likeonInput
for one version but warns about being deprecated and suggests switching toonInput
. In next version it works like the browser but still warns and tells you to useonInput
forever.Optional: Add a getter/setter on DOM
.value
in development mode and warn if this is used directly.Uncontrolled Components
onInput
: Not polyfilled. Works however the browser works. Warns about browser differences if you don't also specifyonClick
,onKeyDown
and/oronKeyUp
. The warnings suggests implementing those listeners to cover more edge cases, or switch to a controlled component.onChange
: Not polyfilled. Works however the browser works.The text was updated successfully, but these errors were encountered: