## React ES6的bind技巧
>React有兩種創建組件的方式。
使用ES6 extends 來創建組件的話, 組件里面的方法,需要
```javascript
this.funName.bind(this)
```
每次為方法添加 bind 方法,很麻煩。
推薦兩個庫,給ES6 帶來自動綁定的方法
## 一、使用裝飾器 decorator
[autobind-decorator](https://github.com/andreypopp/autobind-decorator)
`npm install autobind-decorator`
```javascript
import autobind from 'autobind-decorator'
class Component {
constructor(value) {
this.value = value
}
@autobind
method() {
return this.value
}
}
let component = new Component(42)
let method = component.method // .bind(component) isn't needed!
method() // returns 42
// Also usable on the class to bind all methods
@autobind
class Component { }
```
## 二、使用方法的形式
[React-autobind](https://github.com/cassiozen/React-autobind)
```javascript
import autoBind from 'react-autobind';
...
constructor(props) {
super(props);
autoBind(this);
}
```
## 結束語
了解具體的用法,請到 github 里面看 文檔。