@rx-angular/state


Description
@rx-angular/state
is a library designed to help developers effectively manage component-level state in Angular.
It offers a lightweight and intuitive API and automatic subscription handling, making it a perfect solution for handling state in any Angular component, service or directive.
This library offers unique features such as merging global state into local state, shared state selections, subscription-free interaction, and integration with imperative functions like component lifecycle and HostBindings.
It is an ideal alternative or complimentary library to global state management solutions like Akita, NgRx, and NgXs.
Introduction Video

Install and Update
npm install --save @rx-angular/state
pnpm install --save @rx-angular/state
yarn add @rx-angular/state
For those currently using @rx-angular/state, we recommend updating with the @angular/cli update command to ensure a smooth transition and proper processing of all code migrations.
Simply run the following command:
ng update @rx-angular/state
nx migrate @rx-angular/state
Usage
Local Provider (recommended): Use RxState as a local provider in your component to make use of Angular's Dependency Injection.
With the new inject
method:
@Component({
providers: [RxState],
})
export class RxStateInjectionComponent {
private state: RxState<{ foo: string }> = inject(RxState);
state$ = this.state.select();
}
With constructor based injection:
@Component({
providers: [RxState],
})
export class RxStateInjectionComponent {
state$ = this.state.select();
constructor(private state: RxState<{ foo: string }>) {}
}
Inheritance: Use RxState by extending it in your component.
@Component({
})
export class RxStateInheritanceClass extends RxState<{ foo: string }> {
value$ = this.select();
}
API overview
With @rx-angular/state
, you can easily manage your component state with a range of powerful methods.
You find a detailed API documentation here.
Link an Observable source to your component state and update it with emitted changes.
With three signatures, this method offers automatic subscription handling,
making it easy to merge the source, update specific properties, or map to a projection function.
Get a cached, distinct Observable of your state, with options to access single properties, transform single properties, or transform state slices.
With reactive composition support from rxjs, this method gives you complete control over selected data.
Retrieve your current state in an imperative manner.
Whether you want the entire state or just a part of it, .get()
makes it easy to access your data,
with the ability to access deeply nested values.
Easily update one or many properties of your state. Whether you provide a partial state object or a function to calculate the new value,
.set()
provides two signatures for updating multiple properties or a single property by name.
Manage side-effects of your state with the .hold()
method, which holds the trigger Observable and executes an optional handler function.
With automatic subscription handling, this method is an effective way to manage side-effects without the hassle.
Customize your state accumulation function with .setAccumulator()
.
Whether you want to update the accumulation logic for deep updates or solve immutability problems, this method is a powerful tool for fine-tuning your state management.
Addons
The following complimentary tools are recommended for use with RxState to improve the development experience and optimize application performance.
Reduce the amount of boilerplate code required in templates and bring rendering performance to next level.
Reactively handle side effects, forget about the subscribe
API and potential memory leaks.
Create composable action streams for user interaction and backend communication with a minimal memory footprint.
Simplify data structures management. Create, modify, convert arrays and objects with ease.
Enforce best practices for building reactive, performant, and Zone-less Angular applications.
Optimize state selections and data transfer, ensure only the necessary data is transferred.
Version Compatibility
RxAngular | Angular |
---|
^1.0.0 | >=12.0.0 |
^2.0.0 | >=13.0.0 |
^14.0.0 | ^14.0.0 |
^15.0.0 | ^15.0.0 |
Regarding the compatibility with RxJS, we generally stick to the compatibilities of the Angular framework itself, for more information about the compatibilities of Angular itself see the official guide.
Contribution
If you want to contribute to this project, please follow our guideline.
Additional materials
OSS Example Applications