diff --git a/content/docs/handling-events.md b/content/docs/handling-events.md index 4077e45d49b..9ad18438092 100644 --- a/content/docs/handling-events.md +++ b/content/docs/handling-events.md @@ -11,7 +11,7 @@ redirect_from: Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences: * React events are named using camelCase, rather than lowercase. -* With JSX you pass a function as the event handler, rather than a string. +* With TSX you pass a function as the event handler, rather than a string. For example, the HTML: @@ -23,7 +23,7 @@ For example, the HTML: is slightly different in React: -```js{1} +```typescript @@ -39,7 +39,7 @@ Another difference is that you cannot return `false` to prevent default behavior In React, this could instead be: -```js{2-5,8} +```typescript function ActionLink() { function handleClick(e) { e.preventDefault(); @@ -60,8 +60,11 @@ When using React you should generally not need to call `addEventListener` to add When you define a component using an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), a common pattern is for an event handler to be a method on the class. For example, this `Toggle` component renders a button that lets the user toggle between "ON" and "OFF" states: -```js{6,7,10-14,18} -class Toggle extends React.Component { +```typescript +interface IOwnProps {} +interface IState {} + +class Toggle extends React.Component { constructor(props) { super(props); this.state = {isToggleOn: true}; @@ -70,13 +73,13 @@ class Toggle extends React.Component { this.handleClick = this.handleClick.bind(this); } - handleClick() { + private handleClick() { this.setState(prevState => ({ isToggleOn: !prevState.isToggleOn })); } - render() { + public render() { return (