Performance: Composables for reusable logic
This affects page load speed and interaction responsiveness by controlling how reusable logic impacts component rendering and reactive updates.
import { reactive } from 'vue'; export function useCounter() { const state = reactive({ count: 0 }); return { state }; }
import { reactive } from 'vue'; const state = reactive({ count: 0 }); export function useCounter() { return { state }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global shared reactive state in composable | Low (few DOM nodes) | High (many reactive triggers) | Medium (frequent updates) | [X] Bad |
| Instance-scoped reactive state in composable | Low (few DOM nodes) | Low (limited reactive triggers) | Low (updates scoped) | [OK] Good |
composable in Vue?import { ref } from 'vue';
export function useToggle() {
const isOn = ref(false);
function toggle() { isOn.value = !isOn.value; }
return { isOn, toggle };
}What will be the value of isOn.value after calling toggle() twice starting from initial state?isOn.value is false. Each toggle flips it to the opposite.import { ref } from 'vue';
export function useCount() {
const count = ref(0);
function increment() {
count++;
}
return { count, increment };
}.value property.count++ which tries to increment the ref object itself, not its value. It should be count.value++.Pass the test and this pattern moves unknown