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

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

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