I have a button where I want it to add 1 to the amount of components whenever it is clicked. However, the displayed value did not change but when I type in the variable in the console, it is updated.
https://codepen.io/reonLOW/pen/ExyGyKb
<div x-data="addItem()">
<button @click="addItem()">+ 1</button>
<br>
<span x-text="amountOfComponents"></span>
<br>
<span x-text="itemPrice"></span>
</div>
var amountOfComponents = 0;
var itemPrice = 0;
const addItem = () => {
amountOfComponents += 1;
if (amountOfComponents <= 5) {
itemPrice = 500 + (110 * amountOfComponents)
} else if (amountOfComponents > 5, amountOfComponents <= 10) {
itemPrice = 1000 + (105 * amountOfComponents)
} else if (amountOfComponents > 10) {
itemPrice = 1500 + (90 * amountOfComponents)
}
return {
amountOfComponents,
itemPrice
}
}
Also, how can I run it so that it shows 0 as the initial value? Pardon for my lack of knowledge in JavaScript.