Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

onChange typescript definition problem #137

Closed
zwigglers opened this issue Jan 8, 2020 · 1 comment · Fixed by #139
Closed

onChange typescript definition problem #137

zwigglers opened this issue Jan 8, 2020 · 1 comment · Fixed by #139
Assignees
Labels

Comments

@zwigglers
Copy link

zwigglers commented Jan 8, 2020

There is a problem with the typescript definition for onChange.

<textarea onChange={(event) => setState({value: event.target.value})} />

textarea onChange event: React.ChangeEvent<HTMLTextAreaElement>

<TextareaAutosize onChange={(event) => setState({value: event.target.value})} />

TextareaAutosize onChange event: React.FormEvent<HTMLTextAreaElement>

Consequently, TextareaAutosize event definition is missing out event.target.value

@veej veej self-assigned this Jan 24, 2020
@veej
Copy link
Member

veej commented Jan 27, 2020

Hi @zwigglers, thanks for reporting this issue.
When dealing with events it's always preferable to use currentTarget instead of target
(you can find more details here and here).

TL;DR: the reason why event.target.value is missing out from the react typings is that, in general, it's impossible to statically type event.target (since, in some cases, it could return a child of the component to which the handler is actually attached). Using currentTarget (which is guaranteed to always return the component that is handling the event), is generally a safer and better-typed option.

I'll keep this issue open just to replace e.target with e.currentTarget in the examples.

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

Successfully merging a pull request may close this issue.

4 participants