# RxAngular Integration

There are use cases when is desired to have a transient component state has it's lifetime bound to the component lifecycle. [RxAngular](https://github.com/rx-angular/rx-angular) 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:

```ts
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:

```ts
@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 [here](https://github.com/rx-angular/rx-angular/tree/master/apps/tour-of-heroes-ngxs)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.ngxs.io/v18.0/recipes/intregration-with-rxangular.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
