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

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

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 Report

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