-
Notifications
You must be signed in to change notification settings - Fork 48.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binding, ES6 classes, and onChange #3613
Comments
This sounds like perhaps we should be explicitly calling |
Yup, agreed. |
So just in ReactDOMInput (and wherever else it may appear)? I had seen this, I just wanted to make sure from you guys who know the code backwards and forwards that onChange wasn't being called with "this" for some other reason. |
Yeah, ReactDOMInput, ReactDOMSelect, ReactDOMTextarea, and maybe LinkedValueUtils too (just off the top of my head). |
Cool. I'll work on this. Thanks. |
Thought I was going crazy because there was no error, until I checked the value of 'this'. I'll be watching so I can remove the |
I am also experiencing strange behavior with input |
@Zenwolf you still need to onChange={::this.handleChange} |
@MyBoon In the component's constructor, I bound the function to the component instance: constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
} After doing that, the handler was still never called. |
Has any progress been made on this @skevy? I was considering picking it up but can look for something else if you're making progress on it |
So I wrote a test that I was expecting to fail based on the description: it('should have a this value of undefined if bind is not used', function() {
var unboundInputOnChange = function() {
expect(this).toBe(undefined);
}
var instance = <input type="text" onChange={unboundInputOnChange} />;
instance = ReactTestUtils.renderIntoDocument(instance);
ReactTestUtils.Simulate.change(ReactDOM.findDOMNode(instance));
}); But pleasantly it passes - has this been fixed incidentally or have I missed something? |
It looks like I accidentally fixed this in 52a229f. Want to send a pull request with your test to make sure we don't regress? (You don't need the findDOMNode call at all.) |
I'm still encountering this with React 0.14. |
The bug here was that inputs weren't firing with class Foo extends React.Component {
handleClick = (event) => {
}
render() {}
} |
…omponents add test to show `this` is indeed undefined - closes #3613
@spicyj Thanks. I missed the discussion of (not) autobinding in the React v0.13 release notes: https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding |
Alternatively, you may use the The downside to using It seems to me the most favorable approach would be to use the class-level |
When using ES6 classes, autobinding doesn't happen, as we all hopefully know by now :).
So when I do:
<button onClick={this.handleClick}>Test Button</button>
I'd expect that "this" inside of my
handleClick
function in my class is undefined. This is, in fact, what happens.However, when I do:
<input onChange={this.handleChange} type="checkbox" />
..."this" inside of my
handleChange
function is the input component's constructor.Obviously, if I just bind my functions all the time, the problem is resolved. But since "this.setState" exists on the constructor...I was calling this.setState inside my handleChange function, not getting an error, and nothing was happening. So it was confusing to track down.
Admittedly I'm not immediately sure what to do about this...but I thought I'd make an issue and open it up for discussion.
The text was updated successfully, but these errors were encountered: