Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda