0
0
Vueframework~20 mins

Sharing composables across components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composable Sharing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a shared composable maintain state across components?

Consider a composable that returns a ref counter. If two components use this composable, what will happen to the counter state?

Vue
import { ref } from 'vue';

export function useCounter() {
  const count = ref(0);
  function increment() {
    count.value++;
  }
  return { count, increment };
}
ABoth components share the same counter state and increment together.
BEach component has its own independent counter state.
CThe counter state is reset to 0 every time a component uses the composable.
DThe counter state is shared only if components are nested.
Attempts:
2 left
💡 Hint

Think about what happens when a function returns a new ref each time it is called.

📝 Syntax
intermediate
2:00remaining
Correct way to share reactive state across components using composables

Which option correctly shares a reactive count state across multiple components using a composable?

Vue
import { ref } from 'vue';

// Which composable shares the same count instance across components?
A
const count = ref(0);
export function useCount() { return { count }; }
Bexport function useCount() { return { count: 0 }; }
Cexport function useCount() { const count = ref(0); return { count }; }
Dexport function useCount() { const count = 0; return { count }; }
Attempts:
2 left
💡 Hint

Where should the ref be declared to share state?

🔧 Debug
advanced
2:00remaining
Why does the shared composable state not update in the component?

A composable exports a reactive object declared outside the function. The component uses it but the UI does not update when the state changes. What is the likely cause?

Vue
import { reactive } from 'vue';

const state = reactive({ count: 0 });

export function useState() {
  return state;
}

// In component:
// const state = useState();
// state.count++;
// But UI does not reflect changes.
AThe component must use <code>toRefs</code> to unwrap reactive properties for reactivity.
BThe reactive object must be wrapped in a ref to update the UI.
CThe component did not destructure the reactive object, so reactivity is lost.
DThe reactive object is mutated outside Vue's reactivity system.
Attempts:
2 left
💡 Hint

Think about how Vue tracks reactivity when destructuring reactive objects.

state_output
advanced
2:00remaining
Output of shared composable with singleton pattern

Given this composable and two components using it, what is the final value of count.value after both components call increment() once?

Vue
import { ref } from 'vue';

const count = ref(0);

export function useSingletonCounter() {
  function increment() {
    count.value++;
  }
  return { count, increment };
}

// Component A calls increment()
// Component B calls increment()
Aundefined
B1
C0
D2
Attempts:
2 left
💡 Hint

Remember the shared count is a single reactive reference.

🧠 Conceptual
expert
3:00remaining
Best practice for sharing composable state with reset capability

You want to share a reactive state across components but also allow resetting it to initial values. Which pattern is best?

AUse provide/inject to share state but do not expose reset functionality.
BDeclare reactive state inside composable and recreate it on each call to reset.
CDeclare reactive state outside composable and export a reset function that modifies it.
DUse a global variable and update it directly without Vue reactivity.
Attempts:
2 left
💡 Hint

Think about how to keep one shared reactive state and still allow controlled changes.