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

173
Views
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
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!