Skip to content
Advertisement

Decrement function in Reactjs doesn’t work

I am using ReactJs and React-dom in 17.02 version.

I had the same problem described in this question: a value to increment and decrement using button. I used the solution proposed, changed my previous code.

From this one:

JavaScript

To this one:

JavaScript

But I see this error in the console TypeError: Cannot read properties of undefined (reading ‘quantità’) I know the meaning but I can’t understand WHY I see this error if “quantità” (amount) is a prop set to zero:

JavaScript

Advertisement

Answer

You didn’t show where the reference to card comes from in handleDecrement, but somewhere the reference is changed so it won’t work in indexOf. Instead, create a new list and avoid using mutation.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement