Bind ES6 class methods in constructor
- Dominant language
- JavaScript
- Stars
- 453
- Forks
- 43
- PR merge metrics
- No merged PRs in 30d
Description
Seems like binding class methods in ES6 class constructor doesn't work with react-transform-hmr (which uses react-proxy)
```javascript
class Test extends Component({
constructor(props) {
super(props);
this.state = {open: false};
this.handleMouseDown = this.handleMouseDown.bind(this);
window.testInstance = this;
}
handleMouseDown() {
console.log(window.testInstance === this); // false
this.setState({open: true}); // Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the component.
}
render {
return
}
})
```
Binding in componentWillMount works:
```javascript
class Test extends Component({
constructor(props) {
super(props);
this.state = {open: false};
}
componentWillMount() {
window.testInstance = this;
this.handleMouseDown = this.handleMouseDown.bind(this);
}
handleMouseDown() {
console.log(window.testInstance === this); // false
this.setState({open: true}); // Ok
}
render {
return
}
})
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.