LogoLogo
Latest Release
Latest Release
  • NGXS
    • Overview
  • INTRODUCTION
    • WHY
    • INSTALLATION
    • STARTER KIT
    • SCHEMATICS
  • CONCEPTS
    • STORE
      • Overview
      • Store Schematics
      • Store Options
      • Error Handling
      • Meta Reducers
    • ACTIONS
      • Overview
      • Action Schematics
      • Actions Life Cycle
      • Action Handlers
      • Cancellation
      • Monitoring Unhandled Actions
    • STATE
      • Overview
      • State Schematics
      • Life-cycle
      • Composition
      • Lazy Loading
      • State Operators
      • Custom State Operators
      • Shared State
      • State Token
      • Immutability Helpers
      • Error Handling
      • Sub States
    • SELECT
      • Overview
      • Mapped Sub States
      • Optimizing Selectors
      • Selector Utils
      • Error Handling
      • Signals
      • Select Decorator
  • STYLE GUIDE
  • PLUGINS
    • Overview
    • CLI
    • Logger
    • Devtools
    • Storage
    • Forms
    • Web Socket
    • Router
    • HMR
  • RECIPES
    • Authentication
    • Caching
    • Component Events from NGXS
    • Debouncing Actions
    • Dynamic Plugins
    • Module Federation
    • Unit Testing
    • RxAngular Integration
    • Zoneless Server-Side Rendering
  • COMMUNITY & LABS
    • COMMUNITY
      • FAQ
      • Resources
      • Contributors
      • Contributing
      • Sponsors
    • NGXS LABS
      • Overview
  • DEPRECATIONS
    • Inject Container State Deprecation
    • Sub States Deprecation
    • Select Decorator Deprecation
  • CHANGELOG
Powered by GitBook
On this page
Edit on GitHub
  1. RECIPES

RxAngular Integration

PreviousUnit TestingNextZoneless Server-Side Rendering

Last updated 1 year ago

There are use cases when is desired to have a transient component state has it's lifetime bound to the component lifecycle. provides a solution for this problem that is fully reactive and with focus on runtime performance and template rendering.

In order to leverage this library and all the power of a global state management is possible to integrate RxAngular with NGXS. First, let's bind a value from NGXS state to RxAngular component state:

interface HeroesComponentState {
  heroes: Hero[];
}

const initHeroesComponentState: Partial<HeroesComponentState> = {
  heroes: []
};

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css'],
  providers: [RxState] // <--- This binds RxState to the lifetime of the component
})
export class HeroesComponent {
  heroes: Hero[];

  readonly heroes$: Observable<Hero[]> = this.state.select('heroes');

  constructor(
    store: Store,
    private state: RxState<HeroesComponentState>
  ) {
    const stateHeroes$ = store.select(getHeroes);

    this.state.set(initHeroesComponentState);
    this.state.connect('heroes', stateHeroes$); // <--- Here we connect NGXS with RxAngular
  }
}

It is also important to connect the changes on the component local state to NGXS. For example, let's bind the addHero and deleteHero on the following code:

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css'],
  providers: [RxState]
})
export class HeroesComponent {
  heroes: Hero[];

  readonly add = new Subject<string>();
  readonly delete = new Subject<Hero>();

  constructor(
    private state: RxState<HeroesComponentState>,
    private store: Store
  ) {
    this.state.hold(
      // <--- RxAngular hold will manage the subscription for us
      this.add.pipe(switchMap(name => this.store.dispatch(new AddHero(name)))) // <--- dispatch action to NGXS
    );

    this.state.hold(
      this.delete.pipe(switchMap(hero => this.store.dispatch(new DeleteHero(hero))))
    );
  }
}

That's it! The advantage of this approach is that you can leverage all the reactivity from RxAngular and still manage your global state using NGXS. This allows you to combine the best of both libraries.

To check a full example integrating NGXS with RxAngular checkout this Tour of Heroes example

RxAngular
here