React.FormEventHandler<HTMLElement> or React.ChangeEvent for Input?
See original GitHub issueEnvironment
- Package version(s): “@blueprintjs/core”: “^3.11.0”,
- Browser and OS versions: Google Chrome: Version 71.0.3578.98 (Official Build) (64-bit) OS: Mojave 10.14.2
Question
Over at the Text Input Component,
it is stated that the props
for onChange
should be:
React.FormEventHandler<HTMLElement>
Change event handler. Use event.target.value for new value.
Inherited from IControlledProps.onChange
However when I try to use React.FormEventHandler
for my handleChange
function, this shows up:
There’s a vague answer on stackoverflow which describes
ChangeEvent more suitable for typing form events
However, it does not explain why ChangeEvent is more suitable for typing form events.
Using React.ChangeEvent
, the warning goes away. I am now able to edit my input fields on the UI.
My questions are:
- Should I use
ChangeEvent
orFormEventHandler
for my input? - If it is
FormEventHandler
as suggested by the documentation on Blueprint, how do I deal with the warningTS2339: Property 'target' does not exist on type '(event: FormEvent<HTMLInputElement>) => void'.
- If input fields are supposed to use
React.ChangeEvent
, should we updateIControlledProps.onChange
?
My component code is straight forward
<FormGroup helperText={props.helperText} intent="danger">
<InputGroup
value={props.email}
onChange={props.onChange}
name="email"
/**
* using type of "email" instead of "text"
* Doing so constraints the value to a syntactically valid email address.
* This generally has the format username@hostname.tld.
* */
type="email"
placeholder="Enter e-mail address"
large
required
/>
</FormGroup>
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Typescript input onchange event.target.value - Stack Overflow
Generally event handlers should use e.currentTarget.value , e.g.: const onChange = (e: React.FormEvent<HTMLInputElement>) => { const ...
Read more >React Typescript cheatsheet: form elements and onChange ...
Quick cheat sheet with all the typings used for React forms. All the form elements events are the type React.ChangeEvent , where T...
Read more >Forms and Events - React TypeScript Cheatsheets
List of event types ; ChangeEvent, Changing the value of <input> , <select> and <textarea> element. ; ClipboardEvent, Using copy, paste and cut...
Read more >Type the onChange event of an element in React (TypeScript)
ChangeEvent <HTMLInputElement> . The ChangeEvent type has a target property which refers to the element. The element's value can be accessed on event.target....
Read more >How to create React form with a single change event handler?
An HTML form allows users to enter data using input fields that accept text, password, email, number, color, telephone number, date, etc.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey, @havesomeleeway I think there is one misconception you have -
FormEventHandler
does not refers to the event itself, but it refers to event handler - the function that handles this event. Try this out:Note, that type
React.FormEventHandler<HTMLInputElement>
describes ahandleChange
function which is your event handler. In this case, Typescript will automatically recognizeevent
parameter asReact.FormEvent<HTMLInputElement>
- which is the type you are looking for. Alternatively, you can describe only the event:But I’ll recommend you to go with first solution.
Also, note that you should use
currentTarget
instead oftarget
- when typescript will recognize the type of event, it will actually warn you that name/value does not exist on typeEventTarget
which is the type ofevent.target
Actually, according to
React.InputHTMLAttributes
interface whichInputGroup
extends, you should useReact.ChangeEventHanlder
orReact.ChangeEvent
. I’ve missed that in first response.React.FormEventHadnler
is meant to be used for form specific events likeonSubmit
.I think as a general rule it’s better to add type to the whole function, rather than to individual arguments. This empowers reusability and pays off when you have a function that has multiple arguments. A good example of benefits can be found in
@blueprintjs/select
package. Consider this: