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

183
Visualizações
How to render a react component and replicate them from the click event

I want to make multiple copies of Form2 React Component with click event, but the code not working as I want, I am new in react so can anybody help me.

const Comform = () => {


const Form2 = () => {
       
       
      return(

      <div className="card" id='form-card2'>
         <input className="form-check-input" type="checkbox" value="" id="options-check" onClick={Optionscheck} />
      </div>
      
         
     );
    
    }

const Replica = () {
          
          <Form2/>          
            
}

   return(


        <button type="button" className="btn btn-success" onClick={Replica}>Add</button>
   );

   
}
    
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Keep a state as the replica counter and render the number of items you want using Array.from and Array.prototype.map.

Try like this:

const Form2 = () => {
  return (
    <div className="card" id="form-card2">
      <input
        className="form-check-input"
        type="checkbox"
        value=""
        id="options-check"
        // onClick={Optionscheck}
      />
      abcd
    </div>
  );
};

const Replica = () => {
  const [replicaCount, setReplicaCount] = React.useState(0);

  return (
    <div>
      <button
        type="button"
        className="btn btn-success"
        onClick={() => setReplicaCount((prev) => prev + 1)}
      >
        Add
      </button>
      {Array.from({ length: replicaCount }).map((_, index) => (
        <Form2 key={index}/>
      ))}
    </div>
  );
};

ReactDOM.render(<Replica />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>

about 4 years ago · Santiago Gelvez Relatório

0

Summary

You would have to create a handler for the replica button and a state that tracks how often the button was clicked, so that you are able to render as much form items as the button was clicked.

Example

import { useState } from "react";
import "./styles.css";

const FormItem = () => (
  <div className="formItem">
    <label>Check me</label>
    <input type="checkbox" />
  </div>
);

const ReplicaButton = ({ addFormItem }) => (
  <button onClick={addFormItem}>Add more</button>
);

export default function Form() {
  const [formCount, setFormCount] = useState(1);
  const addFormItem = () => setFormCount(formCount + 1);

  return (
    <div>
      <form className="formius">
        {Array(formCount)
          .fill(null)
          .map(() => (
            <FormItem />
          ))}
      </form>
      <ReplicaButton addFormItem={addFormItem} />
    </div>
  );
}

Edit replicating-form-items

about 4 years ago · Santiago Gelvez 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