gaearon / gaearon/react-proxy

Bind ES6 class methods in constructor

Open
#69 3 comments 9 reactions 0 assignees View on GitHub
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

Test
;
}
})
```

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

Test
;
}
})
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.