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

143
Views
onChange in radio button causing undesired change of values in the state
const [toggleValue, setToggleValue] = useState();
..
..
    {Items.map((item) => (
      <Card onClick={() => setToggleValue(item.id)} key={item.id}>
        <CardHeader text={item.text} />
        <Collapse isOpen={toggleValue=== item.id}>
          <CardBody>
               <FormGroup>
                  <CustomInput value="1" type="radio" id="yes" onChange={handleOptionChange(item.id, '1')} />
                  <CustomInput value="2" type="radio" id="yes" onChange={handleOptionChange(item.id, '2')} />
               </FormGroup>
          </CardBody>
        </Collapse>
     </Card>

It works as intended, but when I use the radio button to choose an option in any of these cards, it automatically collapses the current card and expands the first card. The handleOptionChange function which is triggered on making a radio button selection also changes a different value in the state using useState:

const handleOptionChange = (name, value) => () => {
   const Item = name;
   const numericValue = Number(value);
   setFormData({
      ...data,
      id: Item,
      vote: numericValue,
   });
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

handleOptionChange(item.id, '1') should not be directly called on onChange

const [toggleValue, setToggleValue] = useState();
..
..
    {Items.map((item) => (
      <Card onClick={() => setToggleValue(item.id)} key={item.id}>
        <CardHeader text={item.text} />
        <Collapse isOpen={toggleValue=== item.id}>
          <CardBody>
               <FormGroup>
                  <CustomInput value="1" type="radio" id="yes" onChange={()=>handleOptionChange(item.id, '1')} />
                  <CustomInput value="2" type="radio" id="yes" onChange={()=>handleOptionChange(item.id, '2')} />
               </FormGroup>
          </CardBody>
        </Collapse>
     </Card>
about 4 years ago · Juan Pablo Isaza Report

0

change this

<CustomInput value="1" type="radio" id="yes" onChange={handleOptionChange(item.id, '1')} />

into this

<CustomInput value="1" type="radio" id="yes" onChange={() => handleOptionChange(item.id, '1')} />

Reason for the cause ,properly

For the onChange={() => ....} without () =>is same is you will run the function when component mount without waiting for any user action.

You can test by alerting

onChange={() => alert('will run only when user change')}

...

onChange={alert('Will not wait for user and run when component mount')}

about 4 years ago · Juan Pablo Isaza Report

0

JavaScript event will bubble up to the nearest parent that have a event listener for the event

So in this case when you click on the radio button, it actually trigger an onClick event which bubble up to the onClick event listener of your Card component, thus calling the onClick setToggleValue function

You can add a onClick={(event)=>event.stopPropagation()} to your CustomIInput component to prevent this behavior

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!