Vue 3
Refs & Reactive
Choose between ref and reactive for reactive state.
By EZ4Code Team
reactivityrefreactive
Code
import { ref, reactive, isRef } from "vue";
const count = ref(0); // primitive / object wrapper
console.log(count.value); // access via .value
count.value++;
const state = reactive({
user: { name: "Alice", age: 30 },
todos: []
});
state.user.age = 31; // direct mutation, no .value
// Reactive destructuring loses reactivity; use toRefs
const { user } = state;
console.log(isRef(count)); // trueExplanation
ref wraps any value in a reactive object accessed via .value, ideal for primitives. reactive deeply converts an object so direct property mutations stay reactive. Destructuring reactive objects breaks reactivity; use toRefs to preserve it on each field.
More Vue 3 Snippets
Single-File Component
Define a Vue 3 component with script setup, template, and scoped styles.
Composition API
Organize reactive state, computed, and lifecycle logic by feature.
Computed & Watch
Derive values with computed and react to changes with watch.
Props & Emits
Declare inputs and events, and implement v-model on a component.
Lifecycle Hooks
Register lifecycle callbacks as composable functions.
Slots & Scoped Slots
Distribute content with default, named, and scoped slots.