Skip to content Skip to sidebar Skip to footer

Call 2 Functions Within Onchange Event

I'm a bit stuck with my component, I need to call onChange from props so but also call another f

Solution 1:

You can do it by putting the two functions in double quotes:

<input type="text" value={this.state.text} onChange="this.props.onChange(); this.handleChange();" />

This should work. But it would be better if you call the second function within the first one:

function testFunction() {
    onChange();
    handleChange();
}

Solution 2:

If you want inline solution, you can do something like this:

 <input type="text" value={this.state.text} onInput={this.props.onChange} onChange={this.props.handleChange} />

Difference between onInput and onChage is here:

Difference between "change" and "input" event for an `input` element

Solution 3:

function(e) {this.props.onChange(e); this.handleChange(e)}.bind(this)

You might not need .bind(this), but I suspect you will.

This will create a new function on each render, so it'd be better in certain respects to have that function be a component method, but this should work.

Solution 4:

in react

functionhandleChange1(){ console.log("call 1st function");}
functionhandleChange2(){ console.log("call 2nd function");}

functionhandleChange(){ handleChange1(); handleChange2();}

<input type="text" value={this.state.text} onChange={this.props.handleChange} />

hope you got answer.

Post a Comment for "Call 2 Functions Within Onchange Event"