mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-25 23:05:23 +00:00
Update handling-events.md (#8057)
This commit is contained in:
committed by
Dan Abramov
parent
3af93d6c2e
commit
25a1e5acae
@@ -91,7 +91,7 @@ ReactDOM.render(
|
||||
|
||||
[Try it on CodePen.](http://codepen.io/gaearon/pen/xEmzGg?editors=0010)
|
||||
|
||||
You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) by default. If you forget to bind `this.toggle` and pass it to `onClick`, `this` will be `undefined` when the function is actually called.
|
||||
You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) by default. If you forget to bind `this.handleClick` and pass it to `onClick`, `this` will be `undefined` when the function is actually called.
|
||||
|
||||
This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user