React name change when clicking. state

2

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
    
asked by Tiago Viezzoli 18.12.2018 в 22:18
source

1 answer

2

All the time you have been working, what happens is that you are not visualizing the change because you are not using the name of the state anywhere. If you change the implementation of render method by this:

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>

            {/* Aqui esta el cambio: Tiago => {this.state.nombre} */}
            <p onClick = {this.cnombre}>{this.state.nombre}</p>

            <button onClick = {this.cnombre}>Cambiar</button>
            {this.state.count}
        </div>
    )
}

I hope it works and if so, mark this as the correct (important) answer, if not, let me know what went wrong.

    
answered by 18.12.2018 / 22:54
source