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

96
Visualizações
How to Manipulate Sets of Buttons in React with UseStates?

Hi I am new to react and trying to get buttons to light up when you press them, however they are in sets of buttons and I'd like the other buttons in the set to not also light up when one is pressed.

Currently I used UseState() and it just turns all buttons on and off. What's the best practice work around to this, because creating 30 individual button functions does not seem practical. Buttons

  const [ticket, setTicket]=useState(false)


  const ticketHandler = () => {
    setTicket (!ticket);
  }
  <button onClick={ticketHandler} className={`${ticket? 'is-success': ''} button`}>1-5</button>
  <button onClick={ticketHandler} className={`${ticket? 'is-success': ''} button`}>6-9</button>

in this example both ticket '1-5' and ticket '6-9' light up green

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You should create a separate Button component and keep the light-up logic in it. That will solve your issue.

const Button = ({ buttonText }) => {
  const [ticket, setTicket] = useState(false);

  const ticketHandler = () => {
    setTicket(!ticket);
  };

  return (
    <button
      onClick={ticketHandler}
      className={`${ticket ? "is-success" : ""} button`}
    >
      {buttonText}
    </button>
  );
};

And create buttons like this:

<Button buttonText="1-5"/>
<Button buttonText="6-9"/>
about 4 years ago · Juan Pablo Isaza Relatório

0

You just need to store a value you could use in the state (I used '1-5') and then check against that.

function RadioButtonGroup() {
  const [active, setActive] = useState('');

  const handleClick = (value) => () => {
    setActive(value);
  };

  return (
    <div>
      <button
        className={`${active === '1-5' ? 'is-active' : ''}`}
        onClick={handleClick('1-5')}
      >
        1-5
      </button>
      <button
        className={`${active === '6-9' ? 'is-active' : ''}`}
        onClick={handleClick('6-9')}
      >
        6-9
      </button>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I would suggest to give a value to your button and pass it into your handler.

And assign the value to the ticket instead of making the ticket a Boolean

So,

const ticketHandler = (element) => {
    setTicket(element.value);

      };

const getClass = (condition) => ticket === condition ? 'is-success': ''

Button:

<button onClick={ticketHandler} value ="oneToFive" className={`${getClass("oneToFive")} button`}>1-5</button>
<button onClick={ticketHandler} value="sixToNine" className={`${getClass("sixToNine")} button`}>6-9</button>
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