0
votes

Ajouter deux numéros sans bouton Action 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
1I 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.

0 commentaires

4 Réponses :


0
votes

Vous pouvez simplement définir l'état de résultat lorsque vous mettez à jour votre état NUM1 ou NUM2.

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


2 commentaires

@Tarreq Pardonnez-moi, je ne me calme pas comprendre ce que vous voulez dire là.


Excusez-moi mon frère, j'étais confus, je pensais que vous utilisez cela. State, à l'intérieur de l'objet d'état lui-même, ma faute :)



0
votes

Vous pouvez simplement utiliser:

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


0 commentaires

3
votes

Vous n'avez pas à stocker votre résultat dans votre état, comme vous pouvez le calculer par votre état actuel:

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>
    );
  );
}   


0 commentaires

0
votes

Si vous souhaitez conserver votre structure de composant existant, vous pouvez ajouter un appel à updateresult code> en tant que rappel lors de la réglage de l'état dans updateNum1 code> et updateNum2 code> .

EG P>

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

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


0 commentaires