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

129
Views
How to update an object all elements inside map function in react

I want to update object all elements one by one in map, It is updating only one element of the object not all

 const [docUrls, setDocUrls] = useState({
    imageUrl: "", panUrl: "",statementUrl: ""
  })

response from api

res.data.map(item=>{
          if(item.type === 'image'){
            setDocUrls({...docUrls, imageUrl:item.url})
          }else if(item.type  === 'pan'){
            setDocUrls({...docUrls, panUrl:item.url}) 
          }else                   
            setDocUrls({...docUrls, statementUrl:item.url})
          }})

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

setDocUrls is asynchronous so you're just overwriting docUrls everytime you call it.

Having multiple setDocUrls({...docUrls, imageUrl:item.url}) will just keep overwriting the previous call due to the ...docUrls.

Instead, make a copy of docUrl.

const docUrlCopy = { ...docUrls }
res.data.map(item=>{
      if(item.type === 'image'){
        docUrlCopy.imageUrl = item.url

      }else if(item.type  === 'pan'){
        docUrlCopy.panUrl = item.url
      }else  
        docUrlCopy.statementUrl = item.url                 
      }})
setDocUrls(docUrlCopy) // only 1 call 
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!