A
A
Anya Kolomenskaya2020-10-09 20:54:46
JavaScript
Anya Kolomenskaya, 2020-10-09 20:54:46

How to pass multiple parameters to an event handler in a React component?

How to pass multiple parameters to an event handler in a React component? I tried the option

<input onInput={this.handleEvent.bind(this, "SOME TEXT", 0)} />

and option
<input onInput={this.handleEvent.bind("SOME TEXT", 0)} />

This does not work. The handler function itself might look like this:
handleEvent = (theEvent, theText, theNumber) => {
    alert(theEvent.target.value);
    alert(theText);
    alert(theNumber);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JavaDev, 2020-10-09
@YavaDev

//функция
handleEvent= (arg1, arg2, arg3) => (e) => {
  console.log(e.target.value); //значение инпута, если надо
  console.log(arg1); //аргумент 1
  console.log(arg2); //аргумент 2
  console.log(arg3); //аргумент 3
}

//вызов
<input onInput={this.handleEvent(arg1, arg2, arg3)}/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question