-1
I’m starting with React now, and I had an exercise to do, the exercise consists of "draw out" multiples of 5 from an ATM, but I forgot something in the code, because when I click to calculate, just nothing happens.
Follows the code:
import React, { Component } from "react";
import './App.css';
export default class Caixa_Eletronico extends Component{
constructor(props){
super(props);
this.state = {
valorSaque: 0,
n50: 0,
n20: 0,
n10: 0,
n5: 0,
msg: ''
}
this.alterarValor = this.alterarValor.bind(this);
this.calcular = this.calcular.bind(this);
}
alterarValor(event){
const target = event.target;
this.setState({
valorSaque: target.value
})
}
calcular(){
const dados = this.state;
let valor = parseInt(dados.valorSaque);
if(valor % 5 !== 0){
this.setState({
msg: 'O valor deve ser múltiplo de 5!'
});
} else {
let n50 = parseInt(valor / 50);
valor %= 50;
let n20 = parseInt(valor / 20);
valor %= 20;
let n10 = parseInt(valor / 10);
valor %= 10;
let n5 = parseInt(valor / 5);
this.Setstate({
valorSaque: 0,
n50: 50,
n20: 20,
n10: 10,
n5: 5,
msg: ''
})
}
}
render(){
const {state, props} = this;
return(
<div className="borda largura">
<h2>Caixa Eletrônico</h2>
<div>
<input type="text" onChange={this.alterarValor} placeholder="Deseja sacar quanto?"/>
<button type="button" onClick={this.calcular}>Sacar</button>
</div>
<div>
{state.msg}
</div>
<div>
<strong>Notas de R$ 50,00: {state.n50} </strong><br/>
<strong>Notas de R$ 20,00: {state.n20} </strong><br/>
<strong>Notas de R$ 10,00: {state.n10} </strong><br/>
<strong>Notas de R$ 5,00: {state.n5} </strong><br/>
</div>
</div>
)
}
}
Gyraia Sensei, within the method
calcular()
, Within Him, you wrotethis.Setstate(...
is wrong JSX is case sensitive the correct isthis.setState(...
– Augusto Vasques
To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.
– Bacco
thanks so much for the tip and help @Augustovasques
– Fih Capua