What is a state machine in the context of Vue composables?
A state machine is a way to manage different states and transitions between them in a predictable way, often implemented inside Vue composables to organize component logic clearly.
Click to reveal answer
beginner
Why use composables for state machines in Vue?
Composables let you reuse state machine logic across components, keep code clean, and separate state management from UI, making apps easier to maintain.
Click to reveal answer
intermediate
What Vue Composition API functions are commonly used in state machines?
Functions like ref to hold state, computed for derived states, and watch to react to state changes are commonly used in state machines.
Click to reveal answer
intermediate
How do transitions work in a state machine composable?
Transitions are functions that change the current state to a new state based on rules, ensuring the app moves only between allowed states.
Click to reveal answer
beginner
Give an example of a simple state machine with composables in Vue.
A simple toggle state machine can have states 'on' and 'off' with a transition function to switch between them, implemented using ref and a toggle method inside a composable.
Click to reveal answer
What Vue function is best to hold the current state in a state machine composable?
Aref
Bcomputed
Cwatch
Dprovide
✗ Incorrect
ref holds reactive state values, perfect for the current state in a state machine.
What is the main purpose of transitions in a state machine?
ATo change states following rules
BTo render UI elements
CTo fetch data from server
DTo style components
✗ Incorrect
Transitions control how the state changes, ensuring valid moves between states.
Which Vue Composition API function helps react to state changes in a state machine?
Acomputed
Bref
Cwatch
Dinject
✗ Incorrect
watch runs code when reactive data changes, useful for side effects on state changes.
Why is using composables for state machines better than putting logic directly in components?
AIt makes components slower
BIt allows reuse and cleaner code
CIt hides the UI
DIt disables reactivity
✗ Incorrect
Composables help keep logic reusable and components focused on UI.
What would be a valid state transition in a toggle state machine?
A'on' to 'loading'
B'loading' to 'error'
C'off' to 'error'
D'on' to 'off'
✗ Incorrect
A toggle machine usually switches only between 'on' and 'off' states.
Explain how you would build a simple toggle state machine using Vue composables.
Think about holding the current state and a function to change it.
You got /4 concepts.
Describe the benefits of managing component states with state machines inside composables.
Consider how state machines help organize logic and composables help share it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a state machine in a Vue composable?
easy
A. To manage and control the different states of an application clearly and predictably
B. To style components dynamically based on user input
C. To fetch data from an API asynchronously
D. To create reusable UI components without state
Solution
Step 1: Understand state machine role
A state machine helps track and control states in a clear way, avoiding unpredictable behavior.
Step 2: Relate to Vue composables
Vue composables organize this logic so it can be reused across components easily.
Final Answer:
To manage and control the different states of an application clearly and predictably -> Option A
Quick Check:
State machine purpose = Manage states clearly [OK]
Hint: State machines organize app states clearly, not styling or fetching [OK]
Common Mistakes:
Confusing state machines with styling or data fetching
Thinking composables only create UI components
Assuming state machines handle API calls
2. Which of the following is the correct way to declare a reactive state variable for a state machine in a Vue composable?
easy
A. const state = reactive('idle')
B. const state = ref('idle')
C. const state = computed('idle')
D. const state = watch('idle')
Solution
Step 1: Identify reactive state declaration
In Vue composables, ref is used to create a reactive primitive value like a string.
Step 2: Check other options
reactive is for objects, computed is for derived values, and watch is for side effects, so they are incorrect here.
Final Answer:
const state = ref('idle') -> Option B
Quick Check:
Use ref for reactive primitives [OK]
Hint: Use ref() for reactive simple values like strings [OK]
Common Mistakes:
Using reactive() for primitive values
Confusing computed() with state declaration
Using watch() to declare state
3. Given this Vue composable code, what will be the value of state.value after calling next() twice?
import { ref } from 'vue'
export function useMachine() {
const state = ref('idle')
function next() {
if (state.value === 'idle') state.value = 'loading'
else if (state.value === 'loading') state.value = 'success'
}
return { state, next }
}
medium
A. 'idle'
B. 'loading'
C. 'success'
D. undefined
Solution
Step 1: Trace first next() call
Initial state is 'idle'. Calling next() changes it to 'loading'.
Step 2: Trace second next() call
Now state is 'loading'. Calling next() again changes it to 'success'.
Final Answer:
'success' -> Option C
Quick Check:
Two next() calls move state from idle -> loading -> success [OK]
Hint: Trace state changes step-by-step for each next() call [OK]
Common Mistakes:
Assuming state resets to 'idle'
Forgetting second next() changes state again
Confusing state.value with state itself
4. Identify the error in this Vue composable state machine code:
import { ref } from 'vue'
export function useMachine() {
const state = ref('idle')
function next() {
if (state === 'idle') state.value = 'loading'
else if (state.value === 'loading') state.value = 'success'
}
return { state, next }
}
medium
A. next() function is not returned
B. Missing import of ref from 'vue'
C. Cannot assign to state.value, should assign to state
D. Incorrect comparison: should use state.value instead of state
Solution
Step 1: Check state comparisons
state is a ref object, so to compare its value, use state.value, not state directly.
Step 2: Verify other parts
Import is correct, assignment to state.value is correct, and next() is returned properly.
Final Answer:
Incorrect comparison: should use state.value instead of state -> Option D
Quick Check:
Compare ref values with state.value [OK]
Hint: Always compare ref values with .value, not the ref object [OK]
Common Mistakes:
Comparing ref object instead of its value
Forgetting to return functions from composable
Misassigning to ref object instead of .value
5. You want to extend a Vue composable state machine to handle a 'failure' state after 'loading'. Which change correctly adds this new state transition when next() is called from 'loading'?
hard
A. Add an else if: else if (state.value === 'loading') state.value = 'failure'
B. Replace the existing loading transition with state.value = 'failure' only
C. Add a new function to set state to 'failure' instead of modifying next()
D. Change initial state to 'failure' to handle errors
Solution
Step 1: Understand current transitions
Currently, next() changes 'idle' -> 'loading' -> 'success'. To handle 'failure' after 'loading' when next() is called, add a new else if condition for 'loading' to transition to 'failure'.
Step 2: Identify correct addition
Adding an else if for state.value === 'loading' to set state.value = 'failure' extends the state machine correctly without removing existing transitions.
Final Answer:
Add an else if: else if (state.value === 'loading') state.value = 'failure' -> Option A
Quick Check:
Add new transition for loading -> failure without removing others [OK]
Hint: Add a new else if condition for the failure state [OK]
Common Mistakes:
Adding a duplicate else if for 'loading' which conflicts or is unreachable
Adding separate functions instead of modifying next()
Changing initial state instead of the transition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.