Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
React onClick event working on twice clicks when clicking again

I am using react with typescript. I created one button and now I want to change it to invert its text on onClick event for that I am using useState to change the text of the button, for text inversion, I create one boolean flag.

If I click the button first it works perfectly and when I click again it. It won't work on first click.

const Panel = () => {
    let flag: boolean = false;
    const [btnText, setBtnText] = useState("hello!");

    const changeBtnText = () => {
        if(!playButtonFlag){
            setBtnText("How are you!");
            flag = true;
        }
        else{
            setBtnText("hello!");
            flag = false;
        }
    }
  return <div>
      <Button name={btnText} onClick={changeBtnText}/>
  </div>;
};
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The error can be in the variable you are using to check i.e playButtonFlag. It is better to use it via state: React does not re-render when a common variable's value is changed.

A little help for you using an example below:

import React, {useState} from "react";

export default function App() {
  const [btnText, setBtnText] = useState("hello!");
  const [flag, setFlag] = useState(false);

  const changeBtnText = () => {
      if(!flag)
          setBtnText("How are you!");
      else
          setBtnText("hello!");

      setFlag(!flag);
  }
return <div>
    <button value={btnText} onClick={changeBtnText}>{btnText}</button>
</div>;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your flag let flag: boolean = false; variable will be re-initialized with false after re-render. There is a possible chance that playButtonFlag might be the problem. It is better to maintain a state, if you really want that flag to render changes.

const Panel = () => {
  const [btnText, setBtnText] = useState("hello!");
  const [flag,setFlag] = useState(false);

  const changeBtnText = () => {
      setBtnText(!flag ? "How are you!" : "hello!");
      setFlag(currentFlag => !currentFlag)
    }
  
  return (
    <div>
      <Button name={btnText} onClick={changeBtnText} />
    </div>
  );
};

or you can set the button text based on the current text of the button

const Panel = () => {
  const [btnText, setBtnText] = useState("hello!");

  const changeBtnText = () => {
      setBtnText(btnText === "hello!" ? "How are you!" : "hello!");
    }
  
  return (
    <div>
      <Button name={btnText} onClick={changeBtnText} />
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it with object in a simple way

import { useState } from "react";
import "./styles.css";

const BUTTON_TEXT = {
  "hello!": "How are you!",
  "How are you!": "hello!"
};

const Panel = () => {
  const [btnText, setBtnText] = useState("hello!");

  const changeBtnText = () => {
    setBtnText(BUTTON_TEXT[btnText]);
  };

  return (
    <div>
      <button name={btnText} onClick={changeBtnText}>
        {btnText}
      </button>
    </div>
  );
};

export default function App() {
  return (
    <div className="App">
      <Panel />
    </div>
  );
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda