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

223
Vistas
How to push to a map value array and use it as state in React?

I have the following state in React:

const [tasksMap, setTasksMap] = useState(new Map());

with the following map creation:

tasks.forEach((task) => {
  const key = `${week.year}-${week.weekNo}`;

  if (!tasksMap.has(key)) {
    tasksMap.set(key, []);
  }
  tasksMap.get(key).push(task);
});

How can I set the map tasksMap as the React state?

setTasksMap(tasksMap.set(key, [])); works but has an empty array as value, which does not update when I run tasksMap.get(key).push(task);.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Please try this. Here I used separate variable to store data and set that variable into the state at last.

let mapObj = new Map();

tasks.forEach((task) => {
  const key = `${week.year}-${week.weekNo}`;

  if (!mapObj.has(key)) {
    mapObj.set(key, []);
  }
  mapObj.get(key).push(task);
});

setTasksMap(mapObj); 
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