-
Notifications
You must be signed in to change notification settings - Fork 45.6k
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
Event handler 'this' context when using transferPropsTo #1595
Comments
Note that after my change was made, @sebmarkbage made a change to cache the result of getDefaultProps across instances so now you can't refer to the current instance as <p onMouseOver={this.handleMouseOver}>...</p>
handleMouseOver: function(e) {
if (this.props.onMouseOver) {
return this.props.onMouseOver(e);
}
// default behavior here
} |
I don't understand. Can you show me an example involving getDefaultProps & transferPropsTo, has that changed in any way? |
In getDefaultProps you can't access
then the default props will be looked up only once and shared, instead of twice as they were before. There's no way to do what your original example is aiming for using only transferPropsTo. You have to specify the handler explicitly when rendering. |
Hmmm ok. That's kind of a breaking change. I've seen a lot of people use this pattern to share properties and behaviour between components. How would you recommend architecting the first code example if I wanted to share the Highlight mixin between two different components? I could as you say specify the handlers on the actual components, but in this case it's not so DRY because i have like 20-30 components sharing the same behaviour. Adding a new event handler to that mixin will require me to edit all these components instead of just the mixin. |
It is a breaking change. That's why I'm telling you here and why it'll be mentioned prominently in the release notes. Mixins are generally not the preferred way to share functionality. I don't know you exact use case but you might be able to do something similar with composition like this: var Highlight = React.createClass({
highlight: function() {
},
unhighlight: function() {
},
render: function() {
return (
<div onMouseOver={this.highlight} onMouseOut={this.unhighlight}>
{this.props.children}
</div>
);
}
});
var Paragraph = React.createClass({
render: function() {
return (
<Highlight>
<p>Hello</p>
</Highlight>
);
}
}); |
Hello,
Not sure if this is a bug or a feature :)
I have a set of React components that share the same behaviour for events so I created a mixin and used the getDefaultProps/transferPropsTo methods to share this functionality.
As you can see, when adding the event in JSX the local scope of the event callback is binded to the component, but when using getDefaultProps 'this' points to 'window'.
A workaround is obviously to rebind the scope manually
But I'm wondering if the behaviour of getDefaultProps/transferPropsTo should be more constant with setting the properties in JSX? Or maybe there's a better way to inherit event handlers to components altogether?
Here's a JS fiddle with the above example code: http://jsfiddle.net/kb3gN/2703/
Thanks in advance
Daniel
The text was updated successfully, but these errors were encountered: