Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda