I´m trying to do calculator on react, following a tutorial. I have the same code (I think, at least i have verified over and over) but when I click on the button which has the “onClick” attached to it, the page it doesn´t do anything (when in fact, it should display “AC” in the display )
Here I have 2 statess components:
class Display extends React.Component{
render(){
return (
<p id="display">{this.props.result}</p>
)
}
}
class Keypad extends React.Component{
//anclar funcion a estos botones:
botonPresionado = e => {
this.props.botonPresionado(e.target.name)
}
render(){
return (
<div id="grid2">
<button onClick={this.botonPresionado} id="clear" name="clear">AC</button>
<button id="divide" name="divide">/</button>
<button id="multiply" name="multiply">*</button>
<button id="seven" name="seven">7</button>
<button id="eight" name="eight">8</button>
<button id="nine" name="nine">9</button>
<button id="subtract" name="subtract">-</button>
<button id="four" name="four">4</button>
<button id="five" name="five">5</button>
<button id="six" name="six">6</button>
<button id="add" name="add">+</button>
<button id="one" name="one">1</button>
<button id="two" name="two">2</button>
<button id="three" name="three">3</button>
<button id="equals" name="equals">=</button>
<button id="zero" name="zero">0</button>
<button id="decimal" name="decimal">.</button>
</div>
)
}
}
And here the Stateful Component:
class App extends React.Component {
constructor(props){
super(props)
this.state ={
result: ""
}
this.botonPresionado = this.botonPresionado.bind(this);
}
botonPresionado = nombreBoton => {
this.setState = ({
result: nombreBoton
})
}
render(){
return (
<div className="App">
<Display result={this.state.result} />
<Keypad botonPresionado={this.botonPresionado} />
</div>
);
}
}
Here´s the full code: https://codepen.io/Assblack/pen/XwZMNv