Title: Explicit Tracking for `effect()`: Request for Reconsideration · Issue #56155 · angular/angular · GitHub
Open Graph Title: Explicit Tracking for `effect()`: Request for Reconsideration · Issue #56155 · angular/angular
X Title: Explicit Tracking for `effect()`: Request for Reconsideration · Issue #56155 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Changelog June 2, 2024: Added section on relying on external libraryes Description We have been refactoring an application towards using Angular's Signals. Dur...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Changelog June 2, 2024: Added section on relying on external libraryes Description We have been refactoring an applicat...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Changelog June 2, 2024: Added section on relying on external libraryes Description We have been refactoring an applicat...
Opengraph URL: https://github.com/angular/angular/issues/56155
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Explicit Tracking for `effect()`: Request for Reconsideration","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\r\n\r\ncore\r\n\r\n## Changelog\r\nJune 2, 2024: Added section on relying on external libraryes\r\n\r\n### Description\r\n\r\nWe have been refactoring an application towards using Angular's Signals. During this process, we observed a recurring pattern where effects inevitably require the use of `untracked`. This pattern has revealed several issues:\r\n\r\n1. **Frequent Use of `untracked`:** Developers frequently need to use `untracked` to prevent unintended side effects in signals, making it a standard practice in our codebase.\r\n2. **Error-Prone Nature:** The necessity of `untracked` is not well-known among developers, leading to common errors when it is omitted.\r\n3. **Maintenance Overhead:** Constantly ensuring that `untracked` is correctly applied adds to the maintenance burden and cognitive load on developers.\r\n\r\nTo address these issues, we propose that Angular effects should adopt an explicit tracking model. \r\n\r\n### Proposed solution\r\n\r\n1. **Explicit Declaration:** Developers should explicitly declare dependencies within effects, rather than relying on automatic tracking.\r\n2. **Default Non-Tracking:** By default, effects should not track dependencies unless explicitly specified, reducing the need for `untracked`.\r\n\r\n**Example Scenario:**\r\nCurrently, developers write:\r\n\r\n```typescript\r\n\r\nid = input.required\u003cnumber>();\r\n\r\neffect(() => {\r\n // Part 1: Tracking\r\n const id = this.id();\r\n\r\n // Part 2: Execution\r\n untracked(() => {\r\n this.dataService.load(id);\r\n });\r\n})\r\n```\r\n\r\nWith explicit tracking, it would look like:\r\n\r\n```typescript\r\nid = input.required\u003cnumber>();\r\n\r\neffect(this.id, (id) => {\r\n this.dataService.load(id);\r\n});\r\n```\r\n\r\n**Benefits:**\r\n\r\n1. **Reduced Errors:** Explicit tracking minimizes the risk of overlooking `untracked`, leading to more predictable and stable code.\r\n2. **Improved Developer Experience:** Clearer dependency management simplifies understanding and maintaining the code.\r\n3. **Enhanced Performance:** Explicit tracking can optimize performance by avoiding unnecessary re-computations and side effects.\r\n\r\n**Internal API and Library Ecosystem:**\r\nIt is noteworthy that some parts of the internal Angular API already utilize automatic `untracked`. While this is beneficial, other libraries face similar problems and would benefit if the untracking responsibility was handled by the caller. This approach can provide more consistent and error-free usage across different codebases and libraries.\r\n\r\n** External Libraries:**\r\nIt has been suggested that this change be implemented as an external library instead. Most application developers are not aware of explicit tracking, though, and would not expect implicit tracking, leading to bugs. Relying on an external library requires developers to be aware of the problem and actively seek out a solution.\r\n\r\nReference: https://github.com/angular/angular/issues/56155#issuecomment-2137760839\r\n\r\n**Prior Discussion:**\r\nThis issue was previously raised in \r\n- [Issue #54548](https://github.com/angular/angular/issues/54548#issuecomment-1961712985)\r\n- [PR 54614](https://github.com/angular/angular/pull/54614)\r\n\r\n\r\nThank you for considering this enhancement.\r\n\r\n### Alternatives considered\r\n\r\n- Introducing a fourth \"effect-like\" function with explicit tracking\r\n- Keep it as it is","author":{"url":"https://github.com/rainerhahnekamp","@type":"Person","name":"rainerhahnekamp"},"datePublished":"2024-05-29T13:00:15Z","url":"https://github.com/angular/angular/issues/56155"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:27856d67-3c04-2477-4ba2-4b7796388b53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C6A:3F8AB1:3CA3C6C:4D030F3:6B01EFA1 |
| html-safe-nonce | d8ea0f5b04f5c0727cd350e99472f6ddb459e9fe6ed2dddc3455cc7ca9f852f2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzZBOjNGOEFCMTozQ0EzQzZDOjREMDMwRjM6NkIwMUVGQTEiLCJ2aXNpdG9yX2lkIjoiNTQ1Mzc4NDc3NDU5MjI5NDgxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 232e4ba0868c9b582e76c616457c2a7695d64e5b638ea23177111d0069e5fdaf |
| hovercard-subject-tag | issue:2323271824 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/56155/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0643479e4482b0c3b4c5a7f6305050e2f6ec9b9ae1269ecef65f1137d177884d/angular/angular/issues/56155 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0643479e4482b0c3b4c5a7f6305050e2f6ec9b9ae1269ecef65f1137d177884d/angular/angular/issues/56155 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Changelog June 2, 2024: Added section on relying on external libraryes Description We have been refactoring an applicat... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rainerhahnekamp |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width