close

[Solved] React: trigger onChange if input value is changing by state?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error React: trigger onChange if input value is changing by state? in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How React: trigger onChange if input value is changing by state Error Occurs?

Today I get the following error React: trigger onChange if input value is changing by state? in javascript.

How To Solve React: trigger onChange if input value is changing by state Error ?

  1. How To Solve React: trigger onChange if input value is changing by state Error ?

    To Solve React: trigger onChange if input value is changing by state Error I know what you mean, you want to trigger handleChange by click button.
    But modify state value will not trigger onChange event, because onChange event is a form element event.

  2. React: trigger onChange if input value is changing by state?

    To Solve React: trigger onChange if input value is changing by state Error I know what you mean, you want to trigger handleChange by click button.
    But modify state value will not trigger onChange event, because onChange event is a form element event.

Solution 1

I know what you mean, you want to trigger handleChange by click button.

But modify state value will not trigger onChange event, because onChange event is a form element event.

Solution 2

I think you should change that like so:

<input value={this.state.value} onChange={(e) => {this.handleChange(e)}}/>

That is in principle the same as onClick={this.handleClick.bind(this)} as you did on the button.

So if you want to call handleChange() when the button is clicked, than:

<button onClick={this.handleChange.bind(this)}>Change Input</button>

or

handleClick () {
  this.setState({value: 'another random text'});
  this.handleChange();
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment