I have an array of objects with the below structure, how can I update the object's data (file
) based on one of the object property (name
)?
for example how can I update the object named: test1
with a new file
object?
I have tried that but the correct object isn't being updated, a new object is being pushed to the array instead, what am I doing wrong?
const [files, setFiles] = useState(fileArr)
const setFile = (name, data) => {
setFiles(files => ({...files, [name]: {...files[name], ...data}}))
}
fileArr
array:
const fileArr= [
{
name: 'test1',
status: 'Required',
file: {}
},
{
name: 'test2',
status: 'Required',
file: {}
},
{
name: 'test3',
status: 'Required',
file: {}
}
]