Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

muazzamazaz's avatar

Ref variable unlimited addition of value

Following code results in unlimited addition in total function value

<script setup>
import { ref } from 'vue'

const list = ref([1, 2, 3])

const itemRefs = ref([])
let t=0;

function total(id){
t += id;

return t;
}
</script>

<template>
  <ul>
    <li v-for="item in list">
      {{ total(2) }}
    </li>
  </ul>
</template>

https://streamable.com/0o33en

0 likes
1 reply
RandiMa's avatar

Try this.

<script setup>
import { ref, computed } from 'vue'

const list = ref([1, 2, 3])

const itemRefs = ref([])

const total = computed(() => {
  return list.value.reduce((acc, item) => acc + item, 0)
})
</script>

<template>
  <ul>
    <li v-for="item in list">
      {{ item }}
    </li>
    <p>Total: {{ total }}</p>
  </ul>
</template>

Please or to participate in this conversation.