Upgrade to Pro — share decks privately, control downloads, hide ads and more …

3 Effective Rules for Using Signals in Angular

3 Effective Rules for Using Signals in Angular

Manfred Steyer

November 09, 2024
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. About me… Manfred Steyer, ANGULARarchitects.io (Remote) Angular Workshops and Consulting

    Google Developer Expert for Angular Blog, Books, Articles, and Talks about Angular Manfred Steyer
  2. Component With Signals desserts = signal<Dessert[]>([]); const result = await

    this.dessertService.findAsPromise(originalName); this.desserts.set(result); @for (d of desserts(); track d.id) { <dessert-card [item]="d" /> }
  3. Reactive Flow with Computed desserts = signal<Dessert[]>([]); ratings = signal<DessertIdToRatingMap>({});

    ratedDesserts = computed(() => this.toRated(this.desserts(), this.ratings()));
  4. Reactive Flow with Computed desserts = signal<Dessert[]>([]); ratings = signal<DessertIdToRatingMap>({});

    ratedDesserts = computed(() => this.toRated(this.desserts(), this.ratings()));
  5. Reactive Flow with Computed desserts = signal<Dessert[]>([]); ratings = signal<DessertIdToRatingMap>({});

    ratedDesserts = computed(() => this.toRated(this.desserts(), this.ratings())); originalName = signal(''); englishName = signal(''); Load Desserts
  6. Resource API dessertsResource = resource({ request: this.dessertsCriteria, loader: (param) =>

    { return this.#dessertService.findPromise(param.request); } }); Experimental in Angular 19
  7. Resource API dessertsResource = resource({ request: this.dessertsCriteria, loader: (param) =>

    { return this.#dessertService.findPromise(param.request); } }); dessertsCriteria = computed(() => ({ originalName: this.originalName(), englishName: this.englishName(), })); desserts = this.dessertsResource.value; ratedDesserts = computed(() => this.toRated(this.desserts, …));
  8. Ideal-typical Tasks • Drawing on a canvas • Manual DOM

    manipulations • Logging • Synching with LocalStore Stuff, you cannot do with data binding Rendering (and similar tasks …)
  9. Conclusion Architecture Rule #1 Derive state where possible Architecture Rule

    #2 Avoid effects propagating state and Signal writes Architecture Rule #3 Stores make your reactive flow more manageable