0 day streak
javascript / beginner
Snippet

Reactive Variables with ref()

In Vue's Composition API, the ref() function is used to create reactive variables. When the value of a ref changes, Vue automatically updates the parts of the user interface that depend on it.

snippet.js
javascript
1
2
3
4
5
6
7
import { ref } from 'vue';
const count = ref(0);
function increment() {
count.value++;
}
vue
Breakdown
1
import { ref } from 'vue';
Imports the ref function from the Vue library to enable reactivity.
2
const count = ref(0);
Creates a reactive reference with an initial value of 0.
3
count.value++;
Increments the value property. You must use .value when accessing refs in JavaScript.