Parent component provides a shared reactive state 'selected' to its children via Vue's provide/inject.
Execution Table
Step
Action
State Before
State After
Effect
1
Parent creates reactive 'selected' = null
N/A
selected = null
Shared state ready
2
Parent calls provide('selected', selected)
selected = null
selected provided
Children can access 'selected'
3
Child1 injects 'selected'
selected provided
selected injected
Child1 can read/write 'selected'
4
Child2 injects 'selected'
selected provided
selected injected
Child2 can read/write 'selected'
5
Child1 updates selected.value = 'A'
selected = null
selected = 'A'
All children see selected = 'A'
6
Child2 reads selected.value
selected = 'A'
selected = 'A'
Child2 reacts to selected = 'A'
7
Child3 injects 'selected'
selected = 'A'
selected injected
Child3 sees selected = 'A'
8
Child3 updates selected.value = 'B'
selected = 'A'
selected = 'B'
All children see selected = 'B'
9
Parent and children re-render with selected = 'B'
selected = 'B'
selected = 'B'
UI updates to reflect selection
10
Execution ends
selected = 'B'
selected = 'B'
Pattern complete
💡 Execution stops after children update and react to shared state changes.
Variable Tracker
Variable
Start
After Step 1
After Step 5
After Step 8
Final
selected.value
null
null
'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.