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

170
Views
First object i'm passing to react useState array gives me empty array

I'm using state as array of items

const [files, setFiles] = useState([])

when i put objects to its array first time always gives me empty array, second put gives me array with one file and so one. But jsonimages which i use in setFiles func always have file in first iteration.

if(files.length==0){ 
                    console.log("images")
                    let jsonimages = [{image: {preview: URL.createObjectURL(file)},  file: image_test_src, progress: null, keyvalue:key }]
                    console.log(jsonimages)
                    console.log("files")
                file=jsonimages
            console.log(files)}
                else{
                let jsonimages = {image: {preview: URL.createObjectURL(file)},  file: image_test_src, progress: null, keyvalue:key }
                setFiles(current => [...current, jsonimages ])
            }

also i tried this without if block of checking length of array and had same result

 let jsonimages = {image: {preview: URL.createObjectURL(file)},  file: image_test_src, progress: null, keyvalue:key }
                const updateFiles=[...files,
                jsonimages]
                console.log("update files")
                console.log(updateFiles)
                setFiles(updateFiles)
               

               console.log("files")
               console.log(files)

passing files in the second code gives me following outupts

update files 

Array [ {…} ]

so "jsonimages" have the value

but the state doesnt changed

files react_devtools_backend.js:4049:25

Array []

about 4 years ago · Juan Pablo Isaza
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!