Reputation: 521
I had an onClick event define which was working fine:
onClick={this.props.removeFunctionsData.bind(null,this.props.path,this.props.obj.data_type)}
Now I want to define two events on the same onClick. I tried doing this:
onClick={(e)=>{this._onClose(e);this.props.removeFunctionsData.bind(null,this.props.path,this.props.obj.data_type);}}
But in this case only this._onClose(e) works and not the original one. How can I fix this?
Upvotes: 0
Views: 214
Reputation: 4464
Original answer :
You can execute your function by adding ()
at the end :
onClick={
(e)=>{
this._onClose(e);
this.props.removeFunctionsData.bind(null,this.props.path,this.props.obj.data_type)();
}
}
Edit : as dschu commented, the above code is pretty bad, binding or using arrow function in the render method is a bad practice because it creates a new function each time.
null
as first argument.Here's a better code to do what you need :
Class MyComponent extends React.Component {
// your other class methods
handleClick = (e) => {
this._onClose(e);
this.props.removeFunctionsData(this.props.path,this.props.obj.data_type);
}
render() {
<div onClick={this.handleClick} />
}
}
Upvotes: 0
Reputation: 5362
Inside your component, define a handleClick
function, using the ES6 arrow function syntax. This syntax doesn't create a new context, therefore you don't have to bind this
inside the constructor or when calling it.
class Example extends React.Component {
handleClick = () => this.props.removeFunctionsData(this.props.path, this.props.obj.data_type);
render() {
return (
<ComponentToClick onClick={this.handleClick} />
)
}
}
Upvotes: 1