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

169
Visualizações
how to allow user to just choose one radio button dynamically react

I have a JSON data and I created a dynamic form and I have some radio buttons and I said that if type of question was for example 3, render the RadioBtn component and there are some questions that have type 3. I just want to allow user to choose only one radio button and I tried to get name attribute, but the problem is that when I have 3 questions that have type 3, when I choose one radio button in the first question, and choose for the second question, the answer for the first question is gone!!! and it is because of that all question have name attribute and I don't know what to do!!

const Elements = ({ question, handleChange }) => {
  console.log(question.type);
  switch (question.type) {
    case 0:
    case 1:
      return <Input question={question} handleChange={handleChange} />;
    case 2:
      return <DatePick question={question} handleChange={handleChange} />;
    case 3:
      return <RadioBtn question={question} handleChange={handleChange} />;
  
    default:
      return null;
  }
};

export default Elements;

and this is RadioBtn component:

import React, { useContext } from "react";
import { FormContext } from "./FormContext";

const RadioBtn = ({ question, handleChange }) => {
  console.log("id", question.id);
  return (
    <div>
      <label>{question.text}</label>
      <label style={{ color: "gray", fontSize: "12" }}>
        {question.helptext ? question.helptext : null}
      </label>
      {question.list.map((el) => (
        <div>
          <label>{el}</label>
          <input
            type="radio"
            name="radioBtn"
            value={el}
            onChange={(e) => handleChange(question.id, e)}
          />
        </div>
      ))}
    </div>
  );
};

export default RadioBtn;

about 4 years ago · Juan Pablo Isaza
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