Call 2 functions inside the onChange event

I am a bit stuck with my component, I need to call onChange from the props, so

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

but also call another function in a component with a name handleChange()that updates state.text, I tried

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

but it does not work.

+4
source share
2 answers

You can do this by putting two functions in double quotes:

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

That should work. But it would be better if you called the second function in the first:

function testFunction() {
    onChange();
    handleChange();
}
+10
source

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

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

, , , .

+1

Source: https://habr.com/ru/post/1629005/


All Articles