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

Getting Started with Angular Signals

Getting Started with Angular Signals

This is a slide deck on Angular Signals by Dhananjay Kumar

Dhananjay Kumar

December 16, 2024
Tweet

More Decks by Dhananjay Kumar

Other Decks in Education

Transcript

  1. Signal A Signal is a value that is “reactive,” meaning

    it can notify interested consumers when it changes.
  2. .set() – to change the value directly .update() – to

    compute a new value from the previous one
  3. Computed Signal Computations are lazy Computation are disposed of automatically

    Computations are glitch- free It is memorized It is Read-Only you should not write to other signal here
  4. Effects An effect is a side-effectful operation which reads the

    value of zero or more signals, and is automatically scheduled to be re-run whenever any of those signals changes.
  5. Effects use-cases ✓ Synchronizing data between multiple independent models ✓

    Triggering network requests ✓ Performing custom rendering actions such as <canvas>, charting library, and other third-party UI libraries ✓ Logging data being displayed and when it changes, either for analytics or as a debugging tool.
  6. Root Effects ✓ Propagating state changes to other signals ✓

    Effects Synchronizing state with the backend or some local storage ✓ Rendering not tied to a component ✓ Logging/Debugging
  7. Root Effects ✓ Root effects are scheduled via a macro

    task, on each ApplicationRef.tick ✓ They are queued in FIFO order: effects that become dirty first will execute first ✓ One particular consequence of dirty root effects running until the queue is empty is that you might see effects running kind-of synchronously .
  8. View Effects View effects are effects within the component hierarchy

    and execute as part of change detection cycles.
  9. Scheduling of Effects ✓ Effects run asynchronously during the Change

    Detection Process. ✓ Effects will execute in response to their dependencies changes at some point in the future. ✓ Effects always run at least Once ✓ Effects will execute minimal number of times
  10. Effects The effect() is always alive ✓ An effect created

    within components, directives, or service are destroyed when the same is destroyed. ✓ By default, Angular effects lifespan is linked to the underlying DestroyRef in the framework. ✓ Life span of an effect can be manually controlled by passing manualCleanup value to true
  11. Equality function ✓ The signal function optionally specifies an equality

    comparator function. ✓ The comparator decides whether the newly supplied value is the same or different compared to the current value. ✓ Equality functions can be provided to both writable and computed signals.
  12. Equality function If two values are equal block update of

    signal value stop change propagation
  13. Internal • The value that can deliver change notifications •

    Signal Producers • It represents a reactive context depends on producers • Computed , Effect , Template Consumers
  14. Internal The consumer is called live ✓ It sets the

    consumerIsAlways property of the reactive node is set to true. ✓ It is also a producer, which is depended upon by a live consumer. •live effect •Not live computed
  15. Change Detection – Component Tree Root Component Child Component -1

    CC-11 CC -12 CC-121 Child Component- 2 CC -21 CC- 211 CC-22 User clicks a button
  16. Current Component Root Component Child Component -1 CC-11 CC -12

    CC-121 Child Component- 2 CC -21 CC- 211 CC-22 User clicks a button Root Component Child Component -1 CC-11 CC -12 CC-121 Child Component- 2 CC -21 CC- 211 CC-22
  17. Signal based component Root Component Child Component -1 CC-11 CC

    -12 CC-121 Child Component- 2 CC -21 CC- 211 CC-22 User clicks a button Root Component Child Component -1 CC-11 CC -12 CC-121 Child Component- 2 CC -21 CC- 211 CC-22
  18. Input Signal • Pass signal as input • Required Input

    Signal • Optional Input Signal • Input with Default Value • Input Aliasing • Transform function with input value • The effect function runs side effects whenever the input changes
  19. Thanks Dhananjay Kumar • Email – [email protected] • @debug_mode •

    Contact me for Training and Consulting on Angular. • Talk to me about the Indian Angular community and the ng-India. https://debugmode.net/