What is @ngrx/effects?
@ngrx/effects is a library for managing side effects in Angular applications using the Redux pattern. It allows you to isolate side effects from your components and services, making your code more predictable and easier to test.
What are @ngrx/effects's main functionalities?
Handling Side Effects
This code demonstrates how to handle side effects using @ngrx/effects. The `loadItems$` effect listens for the `loadItems` action, calls a service to fetch data, and then dispatches either a success or failure action based on the result.
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';
@Injectable()
export class MyEffects {
loadItems$ = createEffect(() => this.actions$.pipe(
ofType(MyActions.loadItems),
mergeMap(() => this.myService.getAll().pipe(
map(items => MyActions.loadItemsSuccess({ items })),
catchError(() => of(MyActions.loadItemsFailure()))
))
));
constructor(
private actions$: Actions,
private myService: MyService
) {}
}
Combining Multiple Effects
This code demonstrates how to combine multiple effects in a single class. The `loadItems$` effect handles loading items, while the `updateItem$` effect handles updating an item. Both effects listen for their respective actions and call the appropriate service methods.
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';
@Injectable()
export class MyEffects {
loadItems$ = createEffect(() => this.actions$.pipe(
ofType(MyActions.loadItems),
mergeMap(() => this.myService.getAll().pipe(
map(items => MyActions.loadItemsSuccess({ items })),
catchError(() => of(MyActions.loadItemsFailure()))
))
));
updateItem$ = createEffect(() => this.actions$.pipe(
ofType(MyActions.updateItem),
mergeMap(action => this.myService.update(action.item).pipe(
map(() => MyActions.updateItemSuccess({ item: action.item })),
catchError(() => of(MyActions.updateItemFailure()))
))
));
constructor(
private actions$: Actions,
private myService: MyService
) {}
}
Error Handling
This code demonstrates how to handle errors in effects. The `loadItems$` effect catches any errors that occur during the service call and dispatches a `loadItemsFailure` action with the error information.
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';
@Injectable()
export class MyEffects {
loadItems$ = createEffect(() => this.actions$.pipe(
ofType(MyActions.loadItems),
mergeMap(() => this.myService.getAll().pipe(
map(items => MyActions.loadItemsSuccess({ items })),
catchError(error => of(MyActions.loadItemsFailure({ error })))
))
));
constructor(
private actions$: Actions,
private myService: MyService
) {}
}
Other packages similar to @ngrx/effects
redux-saga
redux-saga is a library that aims to make side effects (e.g., asynchronous actions like data fetching) easier and more manageable in Redux applications. It uses generator functions to handle side effects, which can make the code more readable and easier to test compared to @ngrx/effects.
redux-thunk
redux-thunk is a middleware that allows you to write action creators that return a function instead of an action. This function can then perform asynchronous dispatches. While simpler than @ngrx/effects, it can become harder to manage as the complexity of side effects grows.
mobx
MobX is a state management library that makes it simple to connect the reactive data of your application with the UI. Unlike @ngrx/effects, MobX uses observables to manage state and side effects, which can be more intuitive for some developers.
6.0.0-beta.2 (2018-05-11)
Bug Fixes
- build: Fix UMD global names (#1005) (413efd4), closes #1004
- RouterStore: Reset dispatch-tracking booleans after navigation end (#968) (48305aa)
- Schematics: Add check for app/lib to project helper function (5942885)
- Schematics: Add smart default to blueprint schemas (cdd247e)
- Schematics: Remove aliases for state and stateInterface options (f4520a2)
- Schematics: Update upsert actions for entity blueprint (#1042) (0d1d309), closes #1039
- Schematics: Upgrade schematics to new CLI structure (b99d9ff)
- Store: Fix type annotations for select methods (#953) (4d74bd2)
- StoreDevtools: Refresh devtools when extension is started (#1017) (c6e33d9), closes #508
- Update minimum node version to 8.9.0 (#989) (0baaad8)
Features
BREAKING CHANGES
- Schematics: The action blueprint has been updated to be less generic, with associated reducer and effects updated for the feature blueprint
BEFORE:
export enum UserActionTypes {
UserAction = '[User] Action'
}
export class User implements Action {
readonly type = UserActionTypes.UserAction;
}
export type UserActions = User;
AFTER:
export enum UserActionTypes {
LoadUsers = '[User] Load Users'
}
export class LoadUsers implements Action {
readonly type = UserActionTypes.LoadUsers;
}
export type UserActions = LoadUsers;
- Schematics: Aliases for
state
and stateInterface
were removed due to conflicts with component aliases without reasonable alternatives. - Schematics: Minimum dependency for @ngrx/schematics has changed:
@angular-devkit/core: ^0.5.0
@angular-devkit/schematics: ^0.5.0
<a name="6.0.0-beta.1"></a>