Complete the code to import the Vue Composition API function used to create reactive state.
import { [1] } from 'vue'; export function useCounter() { const count = [1](0); return { count }; }
The ref function creates a reactive reference to a value, perfect for simple state like a counter.
Complete the code to define a state machine with two states: 'idle' and 'loading'.
import { ref } from 'vue'; export function useLoader() { const state = ref('idle'); function startLoading() { state.value = [1]; } return { state, startLoading }; }
To move the state machine from 'idle' to 'loading', we assign the string 'loading' to state.value.
Fix the error in the composable by completing the code to toggle between 'on' and 'off' states.
import { ref } from 'vue'; export function useToggle() { const state = ref('off'); function toggle() { state.value = state.value === 'off' ? [1] : 'off'; } return { state, toggle }; }
The toggle function switches the state from 'off' to 'on' and back. So when current state is 'off', it should change to 'on'.
Fill both blanks to create a computed property that returns true if the state is 'active'.
import { ref, computed } from 'vue'; export function useActiveState() { const state = ref('inactive'); const isActive = computed(() => state.value [1] [2]); return { state, isActive }; }
The computed property checks if state.value is exactly equal to the string 'active'.
Fill the blanks to create a composable that manages a state machine with 'start', 'pause', and 'stop' states and a function to set the state.
import { ref } from 'vue'; export function usePlayer() { const state = ref([1]); function setState(newState) { if (['start', 'pause', 'stop'].includes(newState)) { state.value = [2]; } } return { state, setState }; }
newState.The initial state is set to 'start'. The setState function assigns newState to state.value if it is valid. The options include 'start', 'pause', and 'stop'.