Why Share Data Between Components?
Inputs and outputs only connect parents and children. When components far apart need the same data, such as a cart shown in the header and filled from a product page, passing it through every component in between gets messy. A shared service holds it in one place instead.
Journeyman · Angular · Updated
After this lesson you can
- Explain when data belongs in a shared service instead of being passed through inputs and outputs
Key takeaways
- Inputs and outputs connect a parent only to its direct children, so with them alone, the cart would have to live in
Appand pass through every component in between. - Passing data through components that don't use it is called prop drilling, and it makes every change touch half the app.
- Data that many components far apart need, such as the cart, the signed-in user or settings, belongs in a shared service: the single source of truth.
- Use inputs and outputs between a parent and its direct children, such as a list and its cards, and a service for app-wide data. Most apps use both.
Inputs and outputs work beautifully between a parent and its children. But apps have data that many components need, in different parts of the tree.
The problem
A shop has a cart. The header shows how many items are in it. The product cards have Add to cart buttons. The cart page lists the items.
App
├── Header shows: 3 items in the cart
└── ProductsPage
└── ProductList
└── ProductCard has: Add to cart
With only inputs and outputs, the cart's data would have to live in App, the only component above both. Then:
ProductCardemits "add this" toProductList,- which emits it to
ProductsPage, - which emits it to
App, - which updates the cart, and passes the new count down to
Headeras an input.
ProductList and ProductsPage don't care about the cart at all, yet each needs an output just to pass the message along. Passing data through components that don't use it is called prop drilling, and it makes every change to the cart touch half the app.
The fix: one place for the data
Put the cart in a service. There's one instance for the whole app, so:
ProductCardinjects the cart service and callsadd(product).Headerinjects the same service and shows its count.- Nothing in between knows the cart exists.
CartService ← the one copy of the cart
↑ add() ↑ count()
ProductCard Header
The service is the single source of truth: every component reads the same data, so the header and the cart page can never disagree.
Which to use?
| Use | When |
|---|---|
| Inputs and outputs | A parent and its direct children: a list and its cards, a form and its fields |
| A shared service | Data many components need, far apart: the cart, the signed-in user, settings |
Inputs and outputs keep components reusable: a StarRating that takes an input works anywhere. A service keeps app-wide data in one place. Most apps use both.
Check yourself
1 question to try on the lesson page.
So what
When data is needed all over the app, don't pass it hand to hand through components that don't care: keep it in one shared service, and let each component that needs it ask.