| 1 | <h2>Full example</h2> |
| 2 | <pre><code>import * as React from 'react'; |
| 3 | |
| 4 | interface IState { |
| 5 | clicks: number; |
| 6 | } |
| 7 | |
| 8 | export class Clicker extends React.Component<any, IState> { |
| 9 | constructor(props) { |
| 10 | super(props); |
| 11 | |
| 12 | this.state = { |
| 13 | clicks: 0, |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | public clickHandler = () => { |
| 18 | this.setState({ clicks: this.state.clicks + 1 }); |
| 19 | } |
| 20 | |
| 21 | public render() { |
| 22 | return ( |
| 23 | <div> |
| 24 | <p>You have clicked the button {this.state.clicks} time(s).</p> |
| 25 | <p> |
| 26 | <button onClick={this.clickHandler}>click me</button> |
| 27 | </p> |
| 28 | </div> |
| 29 | ); |
| 30 | } |
| 31 | }</code></pre> |