Consider a Vue 3 component that uses a composable to manage a simple toggle state machine with states 'off' and 'on'. The component shows the current state and a button to toggle it.
What will the component display after clicking the toggle button twice?
import { ref } from 'vue'; function useToggleMachine() { const state = ref('off'); function toggle() { state.value = state.value === 'off' ? 'on' : 'off'; } return { state, toggle }; } export default { setup() { const { state, toggle } = useToggleMachine(); return { state, toggle }; }, template: `<div><p>State: {{ state }}</p><button @click="toggle">Toggle</button></div>` };
Each click toggles the state between 'off' and 'on'. Starting from 'off', two toggles bring it back to 'off'.
The toggle function switches the state from 'off' to 'on' or vice versa. Starting at 'off', clicking twice toggles to 'on' then back to 'off'. So the displayed state after two clicks is 'off'.
Choose the correct Vue composable code that initializes a state machine with three states: 'idle', 'loading', and 'success', starting at 'idle'. It should provide a method to transition to 'loading' and then to 'success'.
Remember to use ref for reactive primitive values and update state.value.
Option C correctly uses ref to create a reactive state and updates state.value. Option C uses reactive but wraps the state in an object which is valid but less direct. Option C uses a plain variable, so changes won't be reactive. Option C tries to assign directly to state instead of state.value, which is incorrect.
Analyze the following composable code for a state machine. What error will it cause when used in a component?
import { ref } from 'vue'; export function useCounterMachine() { const count = ref(0); function increment() { count += 1; } return { count, increment }; }
Check how count is updated inside increment.
The variable count is a constant ref object. The code tries to assign directly to count instead of updating count.value. This causes a 'Assignment to constant variable' error.
Given this composable managing states 'start', 'middle', and 'end' in order, what is the value of status after calling next() three times?
import { ref } from 'vue'; export function useStepMachine() { const states = ['start', 'middle', 'end']; const index = ref(0); const status = ref(states[index.value]); function next() { if (index.value < states.length - 1) { index.value++; status.value = states[index.value]; } } return { status, next }; }
Think about how the index changes and when it stops incrementing.
The next() function increments the index until it reaches the last state. After three calls, the index stops at the last state 'end', so status is 'end'.
Choose the best explanation for why composables are useful when implementing state machines in Vue applications.
Think about how composables help organize and reuse code in Vue.
Composables encapsulate reactive state and logic, making state machines reusable and isolated per component. They do not auto-generate UI, replace reactivity, or enforce compile-time typing.