You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -82,16 +82,16 @@ var Counter = createReactClass({
82
82
});
83
83
```
84
84
85
-
## Autobinding {#autobinding}
85
+
## 自动绑定 {#autobinding}
86
86
87
-
In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically bind `this`to the instance. You'll have to explicitly use `.bind(this)` in the constructor:
87
+
对于使用 ES6 的 class 关键字创建的 React 组件,组件中的方法遵循与常规 ES6 class 相同的语法规则。这意味着这些方法不会自动绑定 `this`到这个组件实例。 你需要在 constructor 中显式地调用 `.bind(this)`:
88
88
89
89
```javascript
90
90
classSayHelloextendsReact.Component {
91
91
constructor(props) {
92
92
super(props);
93
93
this.state= {message:'Hello!'};
94
-
//This line is important!
94
+
//这一行很重要!
95
95
this.handleClick=this.handleClick.bind(this);
96
96
}
97
97
@@ -100,7 +100,7 @@ class SayHello extends React.Component {
100
100
}
101
101
102
102
render() {
103
-
//Because `this.handleClick` is bound, we can use it as an event handler.
103
+
//由于 `this.handleClick` 已经绑定至实例,因此我们才可以用它来处理点击事件
104
104
return (
105
105
<button onClick={this.handleClick}>
106
106
Say hello
@@ -110,7 +110,7 @@ class SayHello extends React.Component {
110
110
}
111
111
```
112
112
113
-
With`createReactClass()`, this is not necessary because it binds all methods:
@@ -132,9 +132,9 @@ var SayHello = createReactClass({
132
132
});
133
133
```
134
134
135
-
This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications.
135
+
这就意味着,如果使用 ES6 class 关键字创建组件,在处理事件回调时就要多写一部分代码。但对于大型项目来说,这样做可以提升运行效率。
136
136
137
-
If the boilerplate code is too unattractive to you, you may enable the **experimental**[Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal with Babel:
>**We also found numerous issues in codebases using mixins, [and don't recommend using them in the new code](/blog/2016/07/13/mixins-considered-harmful.html).**
Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass` lets you use a legacy `mixins`system for that.
One common use case is a component wanting to update itself on a time interval. It's easy to use `setInterval()`, but it's important to cancel your interval when you don't need it anymore to save memory. React provides [lifecycle methods](/docs/react-component.html#the-component-lifecycle) that let you know when a component is about to be created or destroyed. Let's create a simple mixin that uses these methods to provide an easy `setInterval()`function that will automatically get cleaned up when your component is destroyed.
If a component is using multiple mixins and several mixins define the same lifecycle method (i.e. several mixins want to do some cleanup when the component is destroyed), all of the lifecycle methods are guaranteed to be called. Methods defined on mixins run in the order mixins were listed, followed by a method call on the component.
0 commit comments