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

muazzamazaz's avatar

Ref variable generating auto addition in value

Following code results in auto addition in total 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
7 replies
MohamedTammam's avatar

Every time the component renders it the t variable will be 0 again.

Use ref to keep its state

let t= ref(0);

function total(id){
	t.value += id;

	return t;
}
muazzamazaz's avatar

The issue seems to be with my development environment, what I should check at my end

Please or to participate in this conversation.