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

228
Views
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 answers
Answer question

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 Report
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!