Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!