import React, { Component } from 'react';
class App extends React.Component{
constructor(){
super();
this.state ={
num1 : 0,
num2 : 0,
result :0,
}
this.updateNum1=this.updateNum1.bind(this);
this.updateNum2=this.updateNum2.bind(this);
this.updateResult=this.updateResult.bind(this);
};
updateNum1(data){
this.setState({
num1 : data.target.value
});
}
updateNum2(data){
this.setState({
num2 : data.target.value
});
}
updateResult(){
this.setState({
result : parseInt(this.state.num1)+parseInt(this.state.num2)
});
}
render(){
return(
<div>
<input type ="number" value={this.state.num1}
onChange = {this.updateNum1} />
<input type ="number" value={this.state.num2}
onChange = {this.updateNum2} />
{/* <button onClick ={this.updateResult} >Add</button>
*/}
<h1>{this.state.result}</h1>
</div>
);
}
}
export default App;
//Add both the numbers without action button// I want to add both the numbers without the use of button 1
I am trying to add two number by taking default value in input field.How can I add this two number without Add button. Like for example if user change the num1 value to 4 from 0 it should update the value adding number 4 value ( num1= 4 + num2 =0 = result 4). It should auto update the third input field value or text with result value in it.