Title: React to input changes: signal effects vs ngOnChange · Issue #55027 · angular/angular · GitHub
Open Graph Title: React to input changes: signal effects vs ngOnChange · Issue #55027 · angular/angular
X Title: React to input changes: signal effects vs ngOnChange · Issue #55027 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Often in angular, developers write code in order to react to input changes in components. In general, one of the common places used to achieve this...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Often in angular, developers write code in order to react to input changes in components. In general, one o...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Often in angular, developers write code in order to react to input changes in components. In general, one o...
Opengraph URL: https://github.com/angular/angular/issues/55027
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"React to input changes: signal effects vs ngOnChange ","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncore\n\n### Description\n\nOften in angular, developers write code in order to react to input changes in components. In general, one of the common places used to achieve this goal is the life cycle hook ngOnChange. \r\n\r\nTypically, something like: \r\n``` typescript\r\nngOnChanges(changes: SimpleChanges): void {\r\n\r\n // a preliminary \"if\" lets you target only the input named value\r\n if (changes['value']) {\r\n // whenever value changes, do something\r\n }\r\n}\r\n```\r\nNow, it seems to me the way to achieve the same goal with signals is using effects. So something like: \r\n``` typescript\r\nvalueEffect = effect(() => {\r\n\r\n // the initial assignment makes sure the effect tracks value changes,\r\n //\r\n // even if test might not be needed in the untracked func (if you call \r\n // a component method using directly this.value()).\r\n // \r\n // Using untracked makes sure nothing else will be tracked except for value. \r\n // Moreover, I can set other signals within untracked. \r\n const test = this.value();\r\n\r\n untracked(() => {\r\n // whenever value changes, do something\r\n });\r\n });\r\n```\r\n\r\nAssuming the considerations above are correct (otherwise sorry for having missed some important points), I have the feeling the signal alternative is a bit strange. Say: effects have a wider scope compared to ngOnChange, but less intuitive to use for a very common thing like \"react to an input change\". \r\n\r\nI'd personally like to have a more clean approach where (possibly) I clearly state that \"an effect should only track this specific signal(s) changes and react only to that\". \r\n\r\nRecently, I've noticed there've been some tickets around the same topic: #54985 and #54372. \r\n\r\nHere is a not trivial example to illustrate the differences: https://stackblitz.com/edit/stackblitz-starters-ey1c9p\n\n### Proposed solution\n\nI'd personally like to have a more clean approach where (possibly) I clearly state that \"an effect should only track one(many) specific signal(s) changes and react only to that\". \n\n### Alternatives considered\n\nIt's mostly a matter of making the intention \"react to a specific input change\" more clear... no other thing. And again: if I missed some important points, sorry! ","author":{"url":"https://github.com/mauriziocescon","@type":"Person","name":"mauriziocescon"},"datePublished":"2024-03-25T12:03:11Z","url":"https://github.com/angular/angular/issues/55027"}
| 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:b9f847b2-fc3d-dbac-3412-3da3a1776b00 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D92:1CCECA:17E7CE6:1FBEB03:6B021AD1 |
| html-safe-nonce | ca01771451845e1249e06be6a5ff0162743b076474e71ce0b81ba6b9cd6c7afa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDkyOjFDQ0VDQToxN0U3Q0U2OjFGQkVCMDM6NkIwMjFBRDEiLCJ2aXNpdG9yX2lkIjoiODQ4MjA4MTY2NzU0ODUxNTM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e2e7eb2a7ee1f2b067497d0338686afedaa5f821144f920393462fc5b873c359 |
| hovercard-subject-tag | issue:2205592503 |
| 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/55027/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5419ee81430b81e81eb56b5349cc97feaf8918156b031c3af7b142c31e46cd26/angular/angular/issues/55027 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5419ee81430b81e81eb56b5349cc97feaf8918156b031c3af7b142c31e46cd26/angular/angular/issues/55027 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Description Often in angular, developers write code in order to react to input changes in components. In general, one o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mauriziocescon |
| 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