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

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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