user5507535
user5507535

Reputation: 1800

Why can't I get the ID of an event.target in React using TypeScript?

In React 18.2 with TypeScript I have:

export default function App() {
  const clikHandler = (event: React.MouseEvent<HTMLElement>) => {
    console.log(event.target.id); // Property 'id' does not exist on type 'EventTarget'.
  };

  return (
    <div className="App">
      <div id="parent" onClick={clikHandler}>
        <div id="child"></div>
      </div>
    </div>
  );
}

But id does exist. Even if I use HTMLDivElement it still complains.

The event is in a parent DIV element and I want to get the ID of the child element that was clicked.

Upvotes: 0

Views: 693

Answers (0)

Related Questions