Unable to add item to array, React

Hello everyone, I am having a strange issue.

addToCart(e) {
      let tempcart = []

I am trying to add the id of whatever is clicked to an array, when I console.log() it it shows me the id no problem. However when I try and add it to an array, it returns undefined.
What is going on? Why is it not adding the id to the array?

Try console.log(tempcart);.

It would be “this.tempcart” is it were a property of the class (possible here, but probably not the best solution). Here, tempcart is a local variable in the scope of that method, not a property of the class.

And if it were a class property, then the previous line would have had to be tempcart.push(e.target.id) - that’s how you know those two lines are different referrences.

I’ve never had such a fast and easy solution, thank you!