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

128
Vistas
Click Text and Change Text React

I have a question about onClick event using React hooks. I actually want the text to change to another text when user click and when user click again, it goes back to first text. For example user click text "Change First", then the text change to "Change Second". Then if user click the "Change Second", the text change to "Change First". It will change as long as user click the text.

This is my useState function:

const [shopStat, setshopChange] = useState("Change first");

And below is the part where I want to change the text:

<div className="featuredItem">
  <span className="featuredTitle">Your text is</span>
     <div className="featuredMoneyContainer">
        <span
         className="featuredMoney"
         style={{ color: green }}
         onClick={() => setshopChange("Change second")}
         >
         {shopStat}
         </span>
     </div>
</div>

However, the code above only change the text once to "Change Second" and I cannot click the text again to change it back to "Change First". Hope you all can help me. Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Set state functions accept curent state as parameter so you can use it like that.

onClick={() => setshopChange((currentState) =>  currentState === "Change second"  ? "Change First" : "Change second"  )}
about 4 years ago · Juan Pablo Isaza Denunciar

0

import React, {useState} from 'react';
import './App.css';

function App() {
    const [shopStat, setshopChange] = useState(true);
    return (
    <main onClick={() => setshopChange(!shopStat)}>
        {shopStat === true ? "Change first" : "Change second"}
    </main>
 );
}

export default App;
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