@tanstack/angular-store package, which integrates with Angular’s modern signals-based reactivity system.
Installation
Install the Angular adapter package:The
@tanstack/angular-store package re-exports everything from @tanstack/store, so you only need to install the Angular package.Angular Version Requirements
Basic Usage
The primary way to use TanStack Store in Angular is through theinjectStore function:
The injectStore Function
TheinjectStore function subscribes your component to store updates and returns an Angular Signal that automatically updates.
Signature
Parameters
store- The store instance to subscribe toselector- (Optional) A function that selects which part of the state you need. Defaults to returning the entire stateoptions.equal- (Optional) A custom equality function. Defaults toshallowoptions.injector- (Optional) A custom injector. If not provided, must be called within an injection context
Return Value
Returns a readonly AngularSignal<TSelected>. Call it like count() to get the current value.
Injection Context
injectStore must be called within an injection context (constructor, field initializer, or factory function) unless you provide a custom injector.Selector Optimization
The selector function allows you to subscribe to only the parts of state you need:Custom Equality Functions
For complex state selections, provide a custom equality function:Shallow Equality
The package uses shallow equality by default for object comparisons:Complete Example: Todo App
Here’s a complete Todo application using Angular standalone components:Angular-Specific Considerations
Signals Integration
The Angular adapter useslinkedSignal internally:
- Fully integrates with Angular’s signals API
- Works with
computed()andeffect() - Automatically handles cleanup with
DestroyRef
Working with Angular Signals
Dependency Injection
Create injectable services for your stores:Server-Side Rendering (SSR)
TanStack Store works with Angular Universal:Performance Tips
- Use selective subscriptions: Subscribe only to the state you need
- Leverage signals: Combine with
computed()for derived state - Injectable services: Wrap stores in services for better organization
- OnPush change detection: Works perfectly with signals-based stores
Derived Stores
Create derived stores that react to other stores:API Reference
For detailed API documentation, see:- See the API reference for detailed documentation: