Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React TypeScript | Correct Type for Event Handler Prop

My App component passes down an event handler as a prop to a Button Component

// App.js

  public handlePlay = () => {
    this.setState({ ****** })
  }
// render

<Button play={this.handlePlay} />

What is the correct type for the event handler passed via prop i.e. play?

// Button.js
interface ButtontProps {
  play: any //what is the correct type here?
}

export const Button: React.SFC<ButtontProps> = ({ play }) => (
  <button onClick={play}>Play</button>
)

I don't want to use any as that would divert me from applying the correct types for such an instance.

like image 591
Jonca33 Avatar asked Dec 06 '22 10:12

Jonca33


1 Answers

It should most likely be either of the following

(event: React.MouseEvent<HTMLElement>) => void
(event: React.MouseEvent<HTMLInputElement>) => void

You can confirm this by looking at the React typings over here.

like image 197
Christian Ivicevic Avatar answered Dec 09 '22 15:12

Christian Ivicevic