Logo

dev-resources.site

for different kinds of informations.

New in Angular: Bridging RxJS and Signals with toSignal!

Published at
12/17/2024
Categories
angular
rxjs
signals
Author
bndf1
Categories
3 categories in total
angular
open
rxjs
open
signals
open
Author
5 person written this
bndf1
open
New in Angular: Bridging RxJS and Signals with toSignal!

Angular developers, are you ready to simplify your reactive programming? Let me introduce you to toSignal, a game-changing feature that seamlessly connects RxJS Observables with Angular's new Signals API.

🔗 What is toSignal?
It's a function that converts an Observable into a Signal, giving you the best of both worlds - the power of RxJS and the simplicity of Signals.

💡 Key benefits:

  1. Streamlined code: No more async pipe in templates
  2. Automatic subscription management
  3. Use anywhere in your app, not just in templates
  4. Improved performance and change detection

Here's a quick example from a recent project:

export class EpisodesService {
  readonly path = '/episodes';
  readonly #api = inject(ApiService);

  getAllEpisodes(): Signal<Episode[]> {
    return toSignal(
      this.#api.get<Chapter[]>(this.path).pipe(map(EpisodeAdapter)),
      { initialValue: [] },
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

And here's how you can use it in a component:

@Component({
  selector: 'app-episodes-container',
  template: `
    @for (episode of episodes(); track episode.id) {
      <app-episodes-item [episode]="episode" />
    }
  `,
})
export class EpisodesContainerComponent {
  episodes = inject(EpisodesService).getAllEpisodes();
}
Enter fullscreen mode Exit fullscreen mode

We're fetching data from an API, transforming it with EpisodeAdapter, and then wrapping the resulting Observable with toSignal(). Now, any component using this service can easily consume the episode data as a Signal!

In the template, we can directly use episodes() to access the latest value, just like any other Signal!
⚠️ Important Note: The RxJS Interop package (@angular/core/rxjs-interop) is currently available for developer preview. While it's ready for experimentation, be aware that it might undergo changes before reaching a stable release.

🚀 Ready to level up your Angular skills? Start experimenting with toSignal in your projects today!!

What do you think? Want to dive deeper into Signals and RxJS integration? Let me know in the comments!.

signals Article's
30 articles in total
Favicon
New in Angular: Bridging RxJS and Signals with toSignal!
Favicon
A Complete Solution for Receiving Signals with Built-in Http Service in Strategy
Favicon
Vanilla JS Signal implementation
Favicon
How I'm Using Signals to Make My React App Simpler
Favicon
Angular Migrating to Signals: A Paradigm Shift in Change Detection
Favicon
The Problems with Signals: A Tale of Power and Responsibility
Favicon
Angular Signals: From Zero to Hero
Favicon
Mutable Derivations in Reactivity
Favicon
Introducing Brisa: Full-stack Web Platform Framework 🔥
Favicon
Async Derivations in Reactivity
Favicon
Scheduling Derivations in Reactivity
Favicon
Exploring Angular's Change Detection: In-Depth Analysis
Favicon
Understanding Reactive Contexts in Angular 18
Favicon
New Free eBook: Angular Mastery: From Principles To Practice.
Favicon
What's new in Angular 18
Favicon
Using @HostBinding with Signals
Favicon
Angular Inputs and Single Source of Truth
Favicon
Angular Signal Queries with the viewChild() and contentChild() Functions
Favicon
Converting Observables to Signals in Angular
Favicon
Angular Signals: Best Practices
Favicon
Streamlining Communication: New Signals API in Angular 17.3
Favicon
Signal-Based Inputs and the Output Function
Favicon
What's new in Angular 17.3
Favicon
Master Angular 17.1 and 17.2
Favicon
Angular Computed Signal with an Observable
Favicon
Django Signals mastery
Favicon
How to mock NgRx Signal Stores for unit tests and Storybook Play interaction tests (both manually and automatically)
Favicon
Derivations in Reactivity
Favicon
Improve data service connectivity in Signal Stores using the withDataService Custom Store Feature
Favicon
How Signals Can Boost Your Angular Performance

Featured ones: