How to do to click on a button for example, I change the name I have in the state. attached code (there are more things like for example the counter that works for me)
import React, { Component } from 'react';
import estilos from '../css/estilos.css'
class Componente extends Component{
constructor(props){
super(props)
this.state = {
count : 0,
nombre : "Tiago"
}
}
cnombre = (e) => {
e.preventDefault(e)
this.setState((prevState)=>{
return{
nombre : prevState.nombre = "Mariano"
}
})
}
sumar = (e) =>{
e.preventDefault()
this.setState(function(prevState){
return{
count : prevState.count + 1
}
})
}
sumar2 = (e) =>{
e.preventDefault()
this.setState((prevState)=>{
return{
count : prevState.count + 2
}
})
}
restar = (e) =>{
e.preventDefault()
this.setState((prevState)=>{
if(prevState.count >= 1){
return{
count : prevState.count - 1
}
}
})
}
resetear = (e) =>{
e.preventDefault()
this.setState({count:0})
}
render(){
return(
<div>
<a href="#" onClick = {this.sumar} className = "sumar"> Sumar</a>
<a href="#" onClick = {this.restar} className = "restar"> Restar</a>
<a href="#" onClick = {this.resetear} className = "reset"> Resetear</a>
<a href="#" onClick = {this.sumar2} className = "reset"> Sumar de a dos</a>
<p onClick = {this.cnombre}>Tiago</p>
<button onClick = {this.cnombre}>Cambiar</button>
{this.state.count}
</div>
)
}
}
export default Componente