user18119766
user18119766

Reputation:

How to properly type a button click in React?

I have a button, on click on which I need to get the name of the button. How to correctly type event event by typescript? No matter how I tried it, it throws an error.

  const handleSortReports= (event : ??? ) => {
    const {name} = event.target
    console.log(name)
  }

<button id="Period" name="Period" type="button" onClick={handleSortReports}>Test</button>

Upvotes: 0

Views: 63

Answers (1)

Arman
Arman

Reputation: 801

You can use React.MouseEvent

    const handleSortReports= (event : React.MouseEvent<HTMLButtonElement> ) => {
    const button: HTMLButtonElement = event.currentTarget;
    console.log(button.name);
  }

Upvotes: 2

Related Questions