簡體   English   中英

如何推送到,更新反應功能組件對象中的數組內的元素

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

我有一個功能組件,它有一個帶有數組的對象,如上所示。 每次從輸入中選擇新文件時,您如何更新數組。 用於更新添加文件和文件名的 fileOnChange 方法

您可以創建另一個變量來存儲以前的文件名和文件

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


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM