Overview
This example shows how to:- Compose child features using
Scope - Embed child state in parent state
- Route child actions through parent actions
- Share views across feature boundaries
Implementation
Key Concepts
State Composition
The parent state embeds child state as properties:Action Composition
Parent actions contain child actions:The Scope Reducer
TheScope reducer connects parent and child domains:
- Where to find child state in parent state (
\.counter1) - How to embed child actions in parent actions (
\.counter1) - What reducer to run for that domain (
Counter())
View Scoping
Views usescope to focus on child domains:
Store<Counter.State, Counter.Action> from the parent Store<TwoCounters.State, TwoCounters.Action>.
Benefits of Composition
Reusability
TheCounter feature and CounterView are completely reusable. They have no knowledge of the parent feature.
Modularity
Each counter is isolated. Changes to one counter don’t affect the other.Testability
You can test child features in isolation or test the composed feature:Parent Logic
Parents can observe and react to child actions:Source Code
View the complete example in the TCA repository:Next Steps
- Explore collections for composing many features
- Learn about tree-based navigation for complex hierarchies
- See shared state for sharing data across features