简体   繁体   中英

how to push to, update elements inside an array that is in a object of a react functional component

const [Collect, setCollect] = React.useState({
visible: false,
files:[],
uploading: 0,
team:"",
manager:"",
owner:"",
fileNames: [],
status:""

});

function fileOnChange(e){
// here update files and filenames arrays
}

i have a functional component that has an object with arrays as shown above. how can u update the arrays everytime new files selected from the input. the fileOnChange method to update add files and filesnames

You can create another variable to store previous filenames and files

let temp1 = Collect.filenames;
let temp2 = Collect.files;
temp1.push('anotherFilename');
temp2.push('anotherFile');
setCollect({...Collect, filenames: temp1, files: temp2});


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM