Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda