Undefined in React method

0

When I press the "." to add the decimal, the display goes from "0" to "undefined.". That is, add it but the 0 becomes undefined. I do not understand why.

  state = {
    displayValue:"0",
    operator: null,
    pendingOperation: false,
    newValue: null
  };

inputDot(){
const displayValue=this.displayValue;
  this.setState({
    displayValue: displayValue + "."
  }) 
console.log(this.displayValue)
}

<button type="button"onClick={() => this.inputDot()}>.</button> 
    
asked by Hernan Ariel 31.12.2018 в 23:29
source

1 answer

1

The problem is that you are accessing the value incorrectly, you want to access this.displayValue , that property does not exist in the component so it is correct to return undefined , what you want to access is the state of the object, so you must prefix state before displayValue , the line would be as follows:

const displayValue = this.state.displayValue;
    
answered by 01.01.2019 в 02:54