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

200
Views
How to create dynamic forms in React. There are some dynamic fields inside of dynamic fields

I want to create dynamic forms, it has name, desc and multiple sections, each section has multiple questions, each question has multiple options. when I click some buttons, it should create form items dynamically. Such as, firstly I enter detalils for name, desc sections. After I enter details fo question when I saved question it open new form enter another questions. After saving questions, I want to add new section as result structure ob array would like this. It is dynamic when you save question or section, it create another question or section.

const [data, setData] = useState([
    {
      name: "",
      desc: "",
      sections: [
        {
          sectionName: "",
          sectionDesc: "",
          questions: [
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },

          ],
        },
        {
          sectionName: "",
          sectionDesc: "",
          questions: [
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
          ],
        },
      ],
    },
  ]);

For now, I created simple array, but I want to this as dynamic as it like the explation

const [data, setData] = useState([
    {
      name: "",
      desc: "",
      sections: [
        {
          sectionName: "",
          sectionDesc: "",
          questions: [
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
          ],
        },
      ],
    },
  ]);

I need handleChange function and addFields in specific fields, sharing links would be great Thank you.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

for handleChange the cleaniest way is using loadash

 const [data, setData] = useState([
    {
      name: "",
      desc: "",
      sections: [
        {
          sectionName: "",
          sectionDesc: "",
          questions: [
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
          ],
        },
      ],
    },
  ]);

  const handleChange = (path, value) => {
    setData(_.set(data, path, value));
  };

  useEffect(() => {
    handleChange("[0].sections[0].questions[0].answer", "A");
    handleChange("[0].sections[0].questions[1].answer", "A");
  }, []);

  console.log(data);

inserting data in specific fields:

setData((prevState) => [
    ...prevState,
    {
      name: "",
      desc: "",
      sections: [
        {
          sectionName: "",
          sectionDesc: "",
          questions: [
            {
              questionType: "",
              questionText: "",
              options: [],
              answer: "",
            },
          ],
        },
      ],
    },
  ]);
about 4 years ago · Santiago Trujillo 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!