0
0
Vueframework~10 mins

Compound components pattern in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compound components pattern
Parent Component
Provide shared state
Child Components
Child1
Consume shared state
Render UI together
The parent component holds shared state and passes it to child components via context. Children consume this state to work together as one unit.
Execution Sample
Vue
<script setup>
import { ref, provide, inject } from 'vue'
const selected = ref(null)
provide('selected', selected)
</script>
<template>
  <slot />
</template>
Parent component provides a shared reactive state 'selected' to its children via Vue's provide/inject.
Execution Table
StepActionState BeforeState AfterEffect
1Parent creates reactive 'selected' = nullN/Aselected = nullShared state ready
2Parent calls provide('selected', selected)selected = nullselected providedChildren can access 'selected'
3Child1 injects 'selected'selected providedselected injectedChild1 can read/write 'selected'
4Child2 injects 'selected'selected providedselected injectedChild2 can read/write 'selected'
5Child1 updates selected.value = 'A'selected = nullselected = 'A'All children see selected = 'A'
6Child2 reads selected.valueselected = 'A'selected = 'A'Child2 reacts to selected = 'A'
7Child3 injects 'selected'selected = 'A'selected injectedChild3 sees selected = 'A'
8Child3 updates selected.value = 'B'selected = 'A'selected = 'B'All children see selected = 'B'
9Parent and children re-render with selected = 'B'selected = 'B'selected = 'B'UI updates to reflect selection
10Execution endsselected = 'B'selected = 'B'Pattern complete
💡 Execution stops after children update and react to shared state changes.
Variable Tracker
VariableStartAfter Step 1After Step 5After Step 8Final
selected.valuenullnull'A''B''B'
Key Moments - 3 Insights
Why do children share the same 'selected' state?
Because the parent uses provide() to share a single reactive 'selected' object, all children inject() the same reference, so updates by one child affect all others (see execution_table steps 2, 3, 5, 8).
What happens if a child updates 'selected.value'?
The update changes the shared reactive state, triggering reactivity in all components that use it, causing UI updates (see execution_table steps 5, 6, 8, 9).
Can children work independently without the parent providing state?
No, children rely on the parent to provide the shared state via provide/inject. Without it, inject() returns undefined and reactivity breaks (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'selected.value' after step 5?
A'B'
B'A'
Cnull
Dundefined
💡 Hint
Check the 'State After' column at step 5 in the execution_table.
At which step do all children see the updated 'selected' value 'B'?
AStep 5
BStep 3
CStep 8
DStep 1
💡 Hint
Look for when 'selected' changes to 'B' and children react in the execution_table.
If the parent did not call provide(), what would happen at step 3?
AChild1 would inject 'selected' as undefined
BChild1 would create its own 'selected'
CChild1 would throw an error
DChild1 would receive null
💡 Hint
Refer to the explanation in key_moments about provide/inject dependency.
Concept Snapshot
Compound components pattern in Vue:
- Parent uses provide() to share reactive state
- Children use inject() to access shared state
- Children update shared state to communicate
- All components reactively update UI together
- Enables flexible, coordinated component design
Full Transcript
The compound components pattern in Vue lets a parent component hold shared reactive state and pass it to child components using provide and inject. The parent creates a reactive variable, for example 'selected', and calls provide to share it. Child components call inject to access the same reactive variable. When one child updates the shared state, all children and the parent reactively update their UI to reflect the change. This pattern allows multiple components to work together as one unit by sharing state without passing props explicitly. The execution table shows step-by-step how the parent creates and provides the state, children inject and update it, and how the UI updates accordingly. Key moments clarify why children share the same state and what happens if the parent does not provide it. The visual quiz tests understanding of state changes and provide/inject behavior. This pattern helps build flexible, coordinated Vue components.