JAVASCRIPT
Vue 3 Composition API with ref and computed
Learn how to use Vue 3 Composition API with ref and computed properties to manage reactive state and derive new values efficiently.
import { ref, computed } from 'vue';
export default {
setup() {
const count = ref(0);
const doubleCount = computed(() => count.value * 2);
function increment() {
count.value++;
}
return {
count,
doubleCount,
increment
};
}
};
How it works: This snippet demonstrates the fundamental building blocks of the Vue 3 Composition API. We use 'ref' to declare reactive state and 'computed' to create a read-only derived property that updates automatically whenever the underlying reference changes.