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

197
Vistas
REACT - toggle active button class on click

I need 2 buttons, one for the "left" and one for the "right" side. I've made a separate component (subfolder) that i am calling in the main file. When the user clicks on certain button it has to become "active", getting the "square-round--active" class so it shows on the GUI. However currently i am having problem figuring out how to do that on 2 separate buttons, only one of them can be active at the time. In my current code the buttons just switch active from one another when any of them is clicked. How can i solve this ?

    const [isActive, setActive] = useState(false);

    const toggleClass = () => {
        setActive(!isActive);
    };

     <ul class="buttons-in-line no-bullets flex-line">
       <li>
          <button type="button" className={isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
          onClick={toggleClass} >Left</button>
       </li>
       <li>
          <button type="button" className={!isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
          onClick={toggleClass} >Right</button>
       </li>
     </ul>

I am making this as a placeholder for now, but later on i have to adjust the code to work with the backend, sending the users pick (left or right) as boolean value. I am adding the backend code below, if anyone has any idea how to put everything together i would truly appreciate it.

const inoAPI = new konfiguratorreact.client.api.InoAPI();

Getters:
inoAPI.getSestav().isOdpiranjeLevo(); // boolean
inoAPI.getSestav().isOdpiranjeDesno(); // boolean

Setters:
inoAPI.getSestav().setOdpiranjeDesno(callback(success, error)))
inoAPI.getSestav().setOdpiranjeLevo(callback(success, error)))


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

0

You currently don't have any way of keeping track of which button is clicked. Something like this should work.

const ToggleComponent = ({ onToggle }) => {
  const [isActive, setActive] = useState(false);

  const toggleClass = (isLeft) => {
    onToggle(isLeft);

    if (isLeft)
      setActive(true);
    else
      setActive(false);
  };

 <ul class="buttons-in-line no-bullets flex-line">
   <li>
      <button type="button" className={isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
      onClick={() => toggleClass(true)} >Left</button>
   </li>
   <li>
      <button type="button" className={!isActive ? 'square-round square-round--active square-round--min-width' : 'square-round square-round--min-width'}
      onClick={() => toggleClass(false)} >Right</button>
   </li>
 </ul>
}

Parent Component

const ParentComponent = () => {
  [isActive, setIsActive] = useState(false);

  const handleToggle = (isActive) => {
    setActive(isActive);
  }

  return (
    <ToggleComponent onToggle={handleToggle} />
  );
}
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