-
Notifications
You must be signed in to change notification settings - Fork 177
Event handlers complain when attaching component method #37
Comments
Any react component change handler will be called much like this.handleClick.bind(this) or this.handleClick.call(this). Therefore, the easiest way to get around the message is bind a null or the component on which the handler is set for the scope of the handler like: Group({ onClick: this.handleClick.bind(this) }) |
@dmitrif Trying that gives me this: I think the problem comes from ReactART.js line 293. How come listeners are bound to My (quick and hacky) solution takes advantage of ReactART.js line 295: var makeListener = function(f) {
return {
handleEvent: f
};
};
...
<Group onClick={makeListener(this.myClickHandler)} /> |
@ThomWright That .call shouldn't cause the warning. Not sure off the top of my head what causes it; probably something in art internals. |
Really? Maybe I'm missing something (not unlikely!) but the warning says In this case, the |
If you look at the implementation of that warning in the React repo you'll see that it intercepts .bind calls, not .call. It's mostly meant as a warning to prevent people from binding unnecessarily and creating extra garbage. |
Ah, gotcha. Having a quick look at the ART internals it does indeed look like it's calling |
try use null instead of this |
So, when you |
That doesn't sound right. The first arg is used as context (i.e., |
Thanks, @spicyj. I cleared up my misunderstanding. In Chrome, I was realizing I didn't have to explicitly define the |
@ThomWright were you ever able to resolve the bind warning? We're on React 0.12 and it looks the same - the source of the warning comes from https://github.com/sebmarkbage/art/blob/master/dom/dummy.js#L100 - it's binding a function which, in this case, is a React component's function. |
@joshma Nothing better than my |
You can cache the React component instance into a variable, instead of using "this", Like this: render: function() { It works in my project :-) |
I had this error when my function call was like this: |
I have added an onClick handler to the group component but I am getting this warning in my console:
bind(): React component methods may only be bound to the component instance. See Component
Here is a shortened version of my component which exhibits the warning. (Plain JS, no JSX)
I believe it has to do with the fact that Group is a React component and that it should only be binding its own methods to the event listener. Is there a way to put this together without getting this warning?
The text was updated successfully, but these errors were encountered: