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

109
Views
How to fix current radio button to be clicked seprate for seprate question

I'm trying to render some questions to evaluate the old device price. From backend I'm receiving multiple questions and I have to give a radio button to the question for YES/NO to for the selection, But when I click One radio button other buttons also get clicked. How to fix this.

My code.

const [radioValue, setRadioValue] = useState('0')

  const radios = [
    { name: 'Yes', value: '1' },
    { name: 'No', value: '0' }

And this is the rendering part:

            {specialQuestLoading ? (
                <Loader></Loader>
              ) : specialQuestError ? (
                <Message color="danger"> {specialQuestError} </Message>
              ) : (
                <>
                  {specialQuestions ? (
                    specialQuestions.map((question) => (
                      <Card classNames="mx-2 py-2" key={question.id}>
                        {question.question_brand === order.prod_brand ? (
                          <>
                            <Card.Title> {question.question}</Card.Title>
                            <Row>
                              {' '}
                              <Col md={4}> </Col>
                              <ButtonGroup className="mb-2">
                                {radios.map((radio, idx) => (
                                  <ToggleButton
                                    key={idx}
                                    id={`radio-${idx}`}
                                    type="radio"
                                    variant="secondary"
                                    name="radio"
                                    value={radio.value}
                                    checked={radioValue === radio.value}
                                    onChange={(e) => setRadioValue(e.currentTarget.value)}
                                  >
                                    {radio.name}
                                  </ToggleButton>
                                ))}
                              </ButtonGroup>
                              <>
                                <br />
                              </>
                            </Row>
                          </>
                        ) : (
                          <></>
                        )}
                      </Card>
                    ))
                  ) : (
                    <></>
                  )}
                </>
              )}

And this is the problem. Both get checked clicking the one. enter image description here Any body knows how to fix this, Or How can I fix this ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are using single state for every specialQuestions Card. So changing any of the ToggleButton affects every Card Element (specialQuestions).

You should split the Card Element to a separate Component and manage the radioValue state there.

export const Card = ({question}) => {
const [radioValue, setRadioValue] = useState('0')

const radios = [
  { name: 'Yes', value: '1' },
  { name: 'No', value: '0' }

return (
  <Card classNames = "mx-2 py-2" key = {question.id} > 
    {question.question_brand === order.prod_brand ? ( 
      <>
        <Card.Title >
          {question.question}
        </Card.Title>
        <Row>
          {' '}
          <Col md = {4}>< /Col>
          <ButtonGroup className = "mb-2" >
            {radios.map((radio, idx) => (
              <ToggleButton 
                key = {idx}
                id = {`radio-${idx}`}
                type = "radio"
                variant = "secondary"
                name = "radio"
                value = {radio.value}
                checked = {radioValue === radio.value}
                onChange = {
                  (e) => setRadioValue(e.currentTarget.value)
                } 
              >
                {radio.name} 
              </ToggleButton>
            ))
            }
          </ButtonGroup>
          <><br /></>
        </Row>
      </>)
      : ( <></>)
    } 
  </Card>
)
}

And consume it as

specialQuestions.map((question) => (
  <Card question={question} />
)
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!