'first element is not pushing inside empty array in reactJS?
trying to push data coming to "id" to "selectedUserId" array, but first element is not inserting. selectedUserId array does not contain first pushed element, push is only working from second element onwards.
const handleSelectOne=(e)=>{
const id = e.target.value;
console.log(id,"handling")
if(!selectedUserIds.includes(id))
{
selectedUserIds.push(id)
}
else
{
selectedUserIds.pop(id)
}
setCheckedStatusOne(!checkedStatusOne[id])
}
const handleDeleteSelectedClick=() =>{
let deletionItemsNo = selectedUserIds.length
selectedUserIds.map((id)=>{
return console.log(id, "<== element inside selecteduserids array")
})
console.log(deletionItemsNo, "<== number of elements in array")
}
output console output
Solution 1:[1]
.pop() method doesn't accept any arguments. It used for removing the last array item.
To be able to insert element in the beginning you should use .unshift(id)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Georgy |
