L
L
Lenar Fattakhov2016-02-08 13:22:56
JavaScript
Lenar Fattakhov, 2016-02-08 13:22:56

How to bind an event in React?

I want to add an id parameter to the event handler, but leave a reference to the event in order to call stopPropagation().
To do this, I use bind, where I pass event and id. But here's the problem, a completely different event is transmitted, that is, instead of mouse click, I get input. What could be the problem?

<li key={i}>
  <a href="##" onClick={this._onClick.bind(this, event, el.id)}>Какой-то текст</a>
</li>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Izmailov, 2016-02-08
@fr_end

Why such a method? Wouldn't it be easier to do this:

constructor(props){
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
return (event, index, value) => { //Твой код}
}

V
vsuhachev, 2016-02-08
@vsuhachev

<li key={i}>
  <a href="##" onClick={event => this._onClick(event, el.id) }>Какой-то текст</a>
</li>

In general, it is better to use refs

A
Anton, 2016-02-08
@SPAHI4

...

_handleClick = id => {
  return e => {
    e.stopPropagation();
    /* some actions */
  }
}
...
<a onClick={ this._handleClick(el.id) }></a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question