Latest Angular effect() Updates

Code With Bilal
4 min readNov 22, 2024

--

The Angular team continually refines its framework by introducing new features and iterating on existing ones, guided by community feedback. The effect() API, initially launched in developer preview, has seen significant updates in Angular v19, aiming to address real-world issues and improve both functionality and developer experience. Let’s explore the key changes.

Removing allowSignalWrites :

Initially, Angular’s effect() API included a flag called allowSignalWrites to discourage directly modifying signals within effects. While the intention was to promote best practices, real-world usage revealed that the flag added unnecessary friction without achieving its intended goal.

Before the Update (with allowSignalWrites)
After the Update (No allowSignalWrites)

Explanation:

  • The count signal starts at 0.
  • Inside the effect(), if count is less than 5, it increments by 1.
  • The removal of allowSignalWrites simplifies this operation, removing friction for developers.

When to Use Signal Updates in effect()

  • Good Practice: Use effect() for simple reactive operations like synchronizing data or updating other signals based on one signal.
  • Avoid Complex Logic: If logic becomes too complex, consider moving it to computed properties or handling it in the component’s TypeScript code.

Improved Effect Timing :

A major update to effect() addresses how and when effects are executed. Previously, effects were queued as microtasks, which could lead to unpredictable behavior. Now, effects are integrated into Angular’s component lifecycle, running alongside change detection.

Before the Update (Effects as Microtasks)

Problem: If effect() runs before the DOM is fully updated, the offsetWidth might return an incorrect value, leading to bugs in the UI.

After the Update (Effects in the Component Lifecycle)

Benefits:

  • Predictability: The effect() now aligns with Angular’s lifecycle, ensuring the DOM is updated before it executes.
  • Hierarchy Awareness: If this component had child components with their own effects, parent effects would run first, maintaining a clear hierarchy.

2. Hierarchy Awareness: If this component had child components with their own effects, parent effects would run first, maintaining a clear hierarchy.

This change offers multiple benefits:

Predictability: Effects now align more closely with the component hierarchy.

Bug Fixes: Resolves issues where effects ran too early or too late in the lifecycle.

Hierarchy Awareness: Ensures that parent effects execute before child effects within the same component tree.

Impact on Existing Code :

While most implementations will continue to work seamlessly, some scenarios may require adjustments. Key use cases to be mindful of include:

DOM Queries in Effects:

Effects relying on DOM properties (e.g., .offsetWidth) should use the new afterRenderEffect API for proper execution timing.

Observable Timing:

Converting input signals to observables via toObservable() now emits earlier than before, potentially affecting API calls or subscriptions.

Non-Signal Queries:

Effects interacting with non-signal view queries may need adjustments using afterRenderEffect or related tools.

Forms and Reactive Data:

Timing-sensitive interactions with forms or reactive data may require delaying effects until lifecycle hooks like ngAfterViewInit or leveraging the afterNextRender utility.

Testing and Adoption :

During internal testing, the Angular team resolved approximately 100 cases where timing changes impacted code behavior. Most adjustments were minor, with some leading to more accurate application behavior.

For now, the effect() API remains in developer preview through Angular v19. This allows the team to gather further feedback and refine the design before stabilizing the API in a future release.

The Role of Community Feedback :

The evolution of the effect() API underscores the Angular team’s commitment to collaboration. Developer insights and real-world use cases continue to shape the framework, ensuring it meets the needs of its vast and diverse community.

If you’d like to explore these changes, they’re available in the latest release candidate. Dive in, experiment, and share your feedback — it’s invaluable to the continued growth of Angular!

--

--

Code With Bilal
Code With Bilal

Written by Code With Bilal

Software Engineer | Angular | Problem Solver

Responses (4)