简体   繁体   English

添加两个没有动作按钮的数字react.js

[英]Add two number without action button react.js

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 //添加两个数字,不带操作按钮///我想添加两个数字,而不使用按钮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). 例如,如果用户将num1的值从0更改为4,则它应更新加4号值的值(num1 = 4 + num2 = 0 =结果4)。 It should auto update the third input field value or text with result value in it. 它应该自动更新第三个输入字段值或其中包含结果值的文本。

You don't have to store your result in your state, as you can calculate it by your current state: 您不必将结果存储在您的状态中,因为您可以根据当前状态进行计算:

class App extends React.Component {
  constructor() {
    super();

    this.state ={
      num1 : 0,
      num2 : 0,
    }
  }

  updateNum1(event) {
    this.setState({
      num1: event.target.value
    });
  }

  updateNum2(event){
    this.setState({
      num2: event.target.value
    });
  }

  render() {
    const { num1, num2 } = this.state;

    const total = parseInt(num1) + parseInt(num2);

    return (
      <div>
        <input
          type='number'
          value={num1}
          onChange={this.updateNum1.bind(this)}
        />
        <input
          type='number'
          value={num2}
          onChange={this.updateNum2.bind(this)}
        />

        <h1>{total}</h1>
      </div>
    );
  );
}   

You can simply set the result state when you are updating your num1 or num2 state. 您可以在更新num1或num2状态时简单地设置结果状态。

updateNum1(data){
  this.setState({
    num1 : data.target.value,
    result: data.target.value + this.state.num2
  });
}

You are going to do the same with your updateNum2 method as well. 您也将对updateNum2方法执行相同的操作。

or instead of having a result state you can just add them both in your jsx dynamically. 或代替具有结果状态,您可以将它们都动态添加到jsx中。

<h1>Result: {this.state.num1 + this.state.num2}</h1>

instead of 代替

<h1>{this.state.result}</h1>

您可以使用:

<h1>{this.state.num1 + this.state.num2 }</h1>

If you want to keep your existing component structure you could add a call to updateResult as a callback when setting state in updateNum1 and updateNum2 . 如果要保留现有的组件结构,可以在updateNum1updateNum2设置状态时添加对updateResult的调用作为回调。

eg 例如

updateNum1(data){
    this.setState({
        num1 : data.target.value
    }, this.updateResult);
}

updateNum2(data){
    this.setState({
        num2 : data.target.value
    }, this.updateResult);
}

This will call the updateResult after the state has been updated 状态更新后,将调用updateResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM