What is @ngrx/component-store?
@ngrx/component-store is a library for managing local/component state in Angular applications. It provides a simple and reactive way to manage state within Angular components, leveraging RxJS for state management and side effects.
What are @ngrx/component-store's main functionalities?
State Management
This feature allows you to manage the state within a component. The `ComponentStore` class is used to define the state and provide methods to update it.
```typescript
import { ComponentStore } from '@ngrx/component-store';
interface State {
count: number;
}
@Injectable()
class CounterStore extends ComponentStore<State> {
constructor() {
super({ count: 0 });
}
readonly increment = this.updater((state) => ({ count: state.count + 1 }));
readonly decrement = this.updater((state) => ({ count: state.count - 1 }));
readonly reset = this.updater(() => ({ count: 0 }));
}
```
Selectors
Selectors are used to derive and expose slices of the state. The `select` method allows you to create observables that emit state changes.
```typescript
@Injectable()
class CounterStore extends ComponentStore<State> {
constructor() {
super({ count: 0 });
}
readonly count$ = this.select(state => state.count);
}
```
Effects
Effects are used to handle side effects, such as HTTP requests. The `effect` method allows you to define observables that can trigger state changes based on external events.
```typescript
@Injectable()
class CounterStore extends ComponentStore<State> {
constructor(private readonly http: HttpClient) {
super({ count: 0 });
}
readonly loadCount = this.effect((trigger$) =>
trigger$.pipe(
switchMap(() => this.http.get<number>('/api/count').pipe(
tapResponse(
(count) => this.patchState({ count }),
(error) => console.error(error)
)
))
)
);
}
```
Other packages similar to @ngrx/component-store
ngxs
NGXS is a state management library for Angular that is modeled after the Redux pattern. It provides a more opinionated and structured approach to state management compared to @ngrx/component-store, with features like actions, selectors, and middleware.
akita
Akita is a state management library for Angular applications that focuses on simplicity and performance. It provides a more flexible and less opinionated approach compared to @ngrx/component-store, with features like entity stores and query services.
angular-redux
Angular-Redux is a library that integrates Redux with Angular. It provides a way to manage global state using the Redux pattern, which can be more complex and boilerplate-heavy compared to the local state management provided by @ngrx/component-store.
14.0.0-beta.0 (2022-05-30)
Bug Fixes
- store: rename template literal to string literal for createActionGroup (#3426) (7d08db1)
Features
Performance Improvements
- component: reset state / trigger CD only if necessary (#3328) (f5b055b)
BREAKING CHANGES
-
- The context of
LetDirective
is strongly typed when null
or
undefined
is passed as input.
BEFORE:
<p *ngrxLet="null as n">{{ n }}</p>
<p *ngrxLet="undefined as u">{{ u }}</p>
- The type of
n
is any
. - The type of
u
is any
.
AFTER:
<p *ngrxLet="null as n">{{ n }}</p>
<p *ngrxLet="undefined as u">{{ u }}</p>
- The type of
n
is null
. - The type of
u
is undefined
.
Creating actions, reducers, and effects is possible without using the creator syntax is possible.
AFTER:
- All schematics use the non-creator syntax to scaffold the code.
- The option
--creators
(and -c
) is removed from the schematic options. - The
skipTests
option is removed while generating actions.
- Minimum version of Angular has been updated
BEFORE:
Minimum version of Angular was 13.x
AFTER:
Minimum version of Angular is 14.x
- component: The native local rendering strategy is replaced by global
in zone-less mode for better performance.
BEFORE:
The change detection is triggered via changeDetectorRef.detectChanges
in zone-less mode.
AFTER:
The change detection is triggered via ɵmarkDirty
in zone-less mode.
- component: The
$error
property from LetDirective
's view context is
a thrown error or undefined
instead of true
/false
.
BEFORE:
<p *ngrxLet="obs$; $error as e">{{ e }}</p>
e
will be true
when obs$
emits error event.e
will be false
when obs$
emits next/complete event.
AFTER:
<p *ngrxLet="obs$; $error as e">{{ e }}</p>
e
will be thrown error when obs$
emits error event.e
will be undefined
when obs$
emits next/complete event.
<a name="13.1.0"></a>