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

211
Views
How to clone React component and save its state

I need to clone the component and insert it to the current components. I was able to do this, but when I clone the component, the values in the previous component are cleared. I need the values in the previous component to be preserved, and the values in the new cloned component to be empty. How can I do that?

import FirstBlocks from './FirstBlocks';
import SecondBlock from './SecondBlock';
import { useState } from 'react';

const formBlocks = [
  {
    Component: FirstBlock,
    props: {
      title: 'first block',
    },
    id: 1,
  },
  {
    Component: SecondBlock,
    props: {
      title: 'second block',
    },
    id: 2,
  },
];

const Form = () => {
  const [formItems, setFormItems] = useState(formBlocks);

  return (
    <>
      {formItems.map(({ Component, props, id }, index) => {
        const cloneBlock = () =>
          setFormItems((formItems) => {
            const newFormItems = [...formItems];
            newFormItems.splice(index + 1, 0, {
              Component,
              props,
              id: Date.now(),
            });
            return newFormItems;
          });

        return <Component {...props} key={id} cloneBlock={cloneBlock} />;
      })}
    </>
  );
};

export default Form;
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!