你好,我在 react 中構建了一個應用程式,我已經從 api 映射了一些資料,并且想知道如何將輸入中的資料添加到特定元素上?我不知道如何做到這一點我嘗試了幾種不同的方法,我嘗試將映射元素 id 添加到狀態中并檢查 id 是否與元素 id 匹配,但我無法使其作業,我可以得到它將標簽應用于每個元素,但我希望它只添加到您輸入標簽的特定元素。
const [students, setStudents] = useState([]);
const[tag, setTag] = useState([])
function createCard(students, n ){
return(
<Card
key = {n}
id= {n}
firstName = {students.firstName}
lastName = {students.lastName}
pic = {students.pic}
email= {students.email}
company = {students.company}
skill = {students.skill}
grades = {students.grades}
tag={tag}
newTag={newTag}
/>
)}
function newTag(event){
const createdTag = event.target.value;
setTag(createdTag)
console.log(createdTag)
}
uj5u.com熱心網友回復:
如果你想在反應中映射一個陣列,你可以使用這種方法
const [students, setStudents] = useState([]);
{students && students.map ( (student , idx) =>
<Card
key = {idx}
onClick = { (e)=>{
// add on specefic idx
let auxiliaryArray = students
auxiliaryArray = auxiliaryArray.filter( (ele , idx) => {
// add logic here
})
setStudents(auxiliaryArray)
}}
>
)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/444604.html
標籤:反应
