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

108
Vistas
How to update state on time with react-hook

I'm trying to move elements from one array to another (columnsOut to columnsIn), but at the end not all values has been transferred (usually 2 out of 5 elements), so it seems something related to asynchronous state from react and I still do not how to cope with that. Can anyone give me directions?

  columnsOut: [1, 2, 3, 4, 5]
  columnsIn: []


  function handleRemoveAllFilters(event) {
    columnsOut.forEach((col) => (
      setColumnsIn(columnsIn.concat(col))
    ));
    setColumnsOut([]);
  }

After running function:

  columnsOut: []
  columnsIn: [4, 5]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The answer given by @Marian here has also been edited to match something similar to below:

function handleRemoveAllFilters(event) {
  setColumnsIn(prev => ([...prev, ...columnsOut]));
// above will retain existing elements in columnsIn array
// below will discard 
// setColumnsIn([...columnsOut]);
  setColumnsOut([]);
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

Change from setting the value for the setColumnsIn to getting a callback that receives the previousState as argument.

function handleRemoveAllFilters(event) {
    columnsOut.forEach((col) => (
      setColumnsIn((prevColumnsIn) => [...prevColumnsIn, col])
    ));
    setColumnsOut([]);
}
about 4 years ago · Juan Pablo Isaza Denunciar
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