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

180
Views
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 answers
Answer question

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 Report

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