Skip to content
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 that takes e.target.value as argument? #120

Closed
olalonde opened this issue Aug 22, 2019 · 3 comments
Closed

onChange that takes e.target.value as argument? #120

olalonde opened this issue Aug 22, 2019 · 3 comments

Comments

@olalonde
Copy link

Looking at my own React code and React code on Github, it seems like 95% of onChange handlers on inputs only ever use e.target.value. So a lot of code ends up looking doing something like: <input onChange={e => setUsername(e.target.value)} /> instead of simply <input onChange={setUsername} />.

It would make a lot of code more clean/readable if there was a prop that would directly pass e.target.value, e.g. <input onValue={setUsername} />.

Food for thought...

@necolas
Copy link

necolas commented Aug 22, 2019

There's no native equivalent for DOM input, but this is exactly what React Native's TextInput does. You can build your own custom component that does this for React DOM

@necolas
Copy link

necolas commented Aug 22, 2019

There is some related discussion here about how we might add onInput to DOM input types and include just the value as a second argument

@necolas necolas closed this as completed Aug 22, 2019
@everdimension
Copy link

@olalonde For what it's worth, in my opinion the native browser event argument makes more sense than just passing the value. Of course either approach may be abstracted into another, but with current api you can have a single handleChange handler and use it like this:

function handleChange(event) {
  setState({ [event.target.name]: event.target.value });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants