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 []