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

171
Vistas
Toggle between green and red and there will be counter to count number of times toggle is pressed

Hi i am creating a project but not sure how to use the useEffect, the toggle button will toggle between green and red and the same time there is a counter will keep track the number of toggle is pressed and also is there a way to use toggle without using the boolean. thanks

function App() {
  const [isToggled, setToggled] = useState(true);
  const [counterState, setCounterState] = useState(0);
  
  const toggleGreenRed = () => setToggled(isToggled ? 'Green' : 'Red' );
  const incrementCounter = () => setCounterState(counterState + 1);
  
  useEffect(() => {}); 

  return (
    <div>

      <button onClick={toggleGreenRed}>Toggle status</button>
      <h1>{isToggled}</h1>
      <h3>{counterState}</h3>
    </div>
  );
};

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

0

There is a simple way of doing it, without using a useEffect hook.

Here, you can see the example without using useEffect hook.

import { useState, useEffect } from "react";

const App = () => {
  const [isToggled, setToggled] = useState(true);
  const [counterState, setCounterState] = useState(0);

  const toggleGreenRed = () => {
    setToggled(!isToggled);
    setCounterState(counterState + 1);
  };

  return (
    <div>
      <button onClick={toggleGreenRed}>Toggle status</button>
      <h1>{isToggled ? "Green" : "Red"}</h1>
      <h3>{counterState}</h3>
    </div>
  );
};

In here, what I've done so far is: increase the counter on the same time, whenever you press the button.

But if you wants to try with useEffect here is the code:

import { useState, useEffect } from "react";

const App = () => {
  const [isToggled, setToggled] = useState(true);
  const [counterState, setCounterState] = useState(-1);

  const toggleGreenRed = () => setToggled(!isToggled);

  useEffect(() => {
    setCounterState(counterState + 1);
  }, [isToggled]);

  return (
    <div>
      <button onClick={toggleGreenRed}>Toggle status</button>
      <h1>{isToggled ? "Green" : "Red"}</h1>
      <h3>{counterState}</h3>
    </div>
  );
};

In here, you can see, useEffect has isToggled dependency. Which means, whenever the isToggled value change, the useEffect run and increase the value of counter.

You can learn more about useEffect hook from react official doc.

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