Consider a Vue app with many components sharing data. What problem does state management solve?
Think about how multiple parts of an app can share and update the same information.
State management centralizes shared data, making it easier to keep components synchronized and avoid inconsistent views.
In a Vue app without state management, two components update the same data independently. What is the likely result?
Think about what happens when two friends edit the same document without coordination.
Without centralized state, components can have conflicting data, causing inconsistent UI and bugs.
Choose the code that properly creates and shares reactive state using Vue's Composition API.
import { reactive } from 'vue'; const state = reactive({ count: 0 }); export function useCounter() { function increment() { state.count++; } return { state, increment }; }
Look for use of reactive and proper export syntax.
Option D correctly creates a reactive object and exports both state and increment function for sharing.
Given this code, why does the component not re-render when state.count changes?
<template>
<p>{{ state.count }}</p>
<button @click="increment">Add</button>
</template>
<script setup>
import { reactive } from 'vue';
const state = { count: 0 };
function increment() {
state.count++;
}
</script>Check how state is created and if Vue can track its changes.
Without using reactive(), Vue cannot detect changes to state.count, so the UI won't update.
In Vue, both provide/inject and global state management can share data. When is provide/inject the better choice?
Think about the scope and lifetime of data sharing.
provide/inject is best for passing data down a component tree locally, not for global app-wide state.