If you put two functions in onClick in React, only one of them will work

Viewed 100

In the following code, I have two functions in the onClick of a div: () => this.delete(this, this.state.id) and this.props.deleteTodo.bind(this, this.state.id). However, when I put both of them in at the same time, only one of them works.

If I limit it to one of them, it works fine, but why doesn't it work when I add both?

<div onClick={() => this.delete(this, this.state.id), this.props.deleteTodo.bind(this, this.state.id)}>button</div>
2 Answers

You need to wrap them in curly braces and you should end lines with semi colons and not commas:

<div onClick={() => { 
    this.delete(this, this.state.id);
    this.props.deleteTodo.bind(this, this.state.id); 
}}>button</div>

For better readability and to avoid errors such as syntax errors, combine them both in a single function and call that function or as simnys answer.

Related