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

Add types for currentTarget on event handlers for IntrinsicElem… #2084

Merged
merged 2 commits into from
Nov 3, 2019
Merged

Add types for currentTarget on event handlers for IntrinsicElem… #2084

merged 2 commits into from
Nov 3, 2019

Conversation

loucyx
Copy link

@loucyx loucyx commented Nov 2, 2019

Currently when doing something like:

const [value, setValue] = useState("");

return (
  <input onInput={event=>setValue(event.currentTarget.value)} />
);

We get an error because value is not defined on type EventTarget. With the change introduced in this PR, the currentTarget in this case is of type HTMLInputElement, and the same happens with all intrinsic elements.

@loucyx
Copy link
Author

loucyx commented Nov 2, 2019

Fixed to work with SVG elements as well.

@coveralls
Copy link

coveralls commented Nov 2, 2019

Coverage Status

Coverage remained the same at 99.891% when pulling d253353 on lukeshiru:jsx-event-target into 3664bd5 on preactjs:master.

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is amazing!! Thank you so much for the PR 👍 ❤️

@marvinhagemeister marvinhagemeister changed the title Add types for currentTarget on event handlers for IntrinsicElements Add types for currentTarget on event handlers for IntrinsicElem… Nov 3, 2019
@marvinhagemeister marvinhagemeister merged commit 5e3ba3d into preactjs:master Nov 3, 2019
@malte-wessel
Copy link

how about the target property? I'm still struggling with making this work:

const [value, setValue] = useState("");

return (
  <input onInput={event=>setValue(event.target.value)} />
);

@marvinhagemeister
Copy link
Member

marvinhagemeister commented Nov 13, 2019

@malte-wessel can you open a new issue? Closed issues or PRs quickly gets hidden in GitHub's UI.

@loucyx
Copy link
Author

loucyx commented Nov 13, 2019

@malte-wessel you need to consider that while currentTarget is always the element that added the eventListener, target can be a child element, so:

  1. Is a good practice to avoid target altogether and just use currentTarget.
  2. If you really need target and you are sure a children will not trigger that event, then you can typecast that yourself in your use case:
<div onClick={event => {
  (event.target as HTMLDivElement).propertyYouWantToAccess
}></div>

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

Successfully merging this pull request may close these issues.

4 participants