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

132
Visualizações
How Can I create a select input tag using Buttons and store the selected one in state

Here is a snippet of 3 buttons

  const [role, setRole] = React.useState('')
  const [active, setActive] = React.useState(false)

 <Grid container spacing={1}>
   <Grid item xs={4}>
     <Button variant='plain' value={role} onClick={e => setState(e.target.value)}>
       User
     </Button>
    </Grid>
  <Grid item xs={4}>
     <Button variant='plain' value={role} onClick={e => setState(e.target.value)}>
       Admin
      </Button>
  </Grid>
  <Grid item xs={4}>
    <Button variant='plain' value={role} onClick={e => setState(e.target.value)}>
       Super Admin
    </Button>
  </Grid>

What I want to do is toggle between buttons, such that when one is selected, the other 2 active to false so i can change the color of the active and inactive buttons

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

0

I think you are using the useState hook wrong. In the beginning, you set up states for role and active, but then you don't use their set methods (which would be setRole and setActive). Instead, you use setState, which i don't think is defined here.

Apart from that, it seems like you want to track active, as a boolean. That's not going to work, since you need to know which one of the three buttons is active.

I suggest changing the state of active to an integer. This way, you can number the buttons, and safe the number.

So in each onClick method, you call setActive() with the correct button number.

E.g.: For button 1:

setActive(1);

I've tried to show what I mean here:

codePen Button Toggle Example

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