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

282
Vistas
React - useState not updating unless class object is duplicated

I am trying to update the state using the useState hook for React. However, I think I am using it incorrectly or at least not how it's intended.

I have a class which is initialised.

const [form, setForm] = useState(new Form());

I am then adding elements to this form:

const element = ...

setForm(form => ({
  ...form,
  elements: [
    ...form.elements,
    element
  ]
});

However, this doesn't seem to update the UI to reflect this change.

I have also tried the following:

setForm(form => {
  form.elements.push(element);
  return form;
});

The only way I can get it to work is doing the following and as you can imagine it becomes quite a mountain to climb as it's a bit all over the place:

const duplicated = JSON.parse(JSON.stringify(form));
duplicated.elements.push(element);
setForm(new Form(duplicated));

Is there a better approach to do this rather than having to duplicate the object everytime.

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