在新的 React ES6 类中this
需要按照此处所述进行绑定:http : //facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding
例如:
class Counter extends React.Component {
constructor() {
super();
this.tick = this.tick.bind(this);
}
tick() {
...
}
...
}
对此的解释是因为它是默认行为,但是如果我创建一个 ES6 类,然后创建它的一个新实例this
将被绑定
import React from 'React'
class Test extends React.Component {
constructor() {
super()
}
foo () {
console.log('bar')
}
hello() {
this.foo()
}
}
var test = new Test()
test.hello()
// > bar
那么为什么 React 中需要绑定呢?