-
-
Notifications
You must be signed in to change notification settings - Fork 2k
docs(signals): apply new naming convention from Angular styleguide #4790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ Deploy Preview for ngrx-io ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for ngrx-site-v19 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@@ -147,7 +147,7 @@ export class BooksComponent implements OnInit { | |||
) | |||
); | |||
|
|||
ngOnInit(): void { | |||
constructor() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question for my understanding of the new guide.
When is the constructor preferred over the lifecycle hook?
(because I noticed other samples that make use of the OnInit hook)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven’t found anything in the documentation that explicitly recommends preferring the constructor over lifecycle hooks. The closest reference is this guideline about keeping lifecycle methods short: https://next.angular.dev/style-guide#keep-lifecycle-methods-simple
There are also some hooks which will get deprecated for Signal Components, but OnInit
is not among them: angular/angular#49682
That said, I personally default to using the constructor—unless I need to implicitly access an InputSignal. The main reason is that the constructor allows for early field initialization and any setup that needs to happen as soon as possible. It also runs within the injection context, which can be important for certain use cases.
Summary: I'm fine for moving to constructor, even if it is not listed in the new style guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When is the constructor preferred over the lifecycle hook?
When a reactive method is called with a signal or observable, it should be called within the injection context (constructor). Otherwise, the warning will be displayed in development mode.
At the time when I wrote this guide, we didn't have this warning. The guide is now in accordance with the latest changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks for both answers 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rename Numbers
to NumbersDoubler
. Todos
and Counter
are good names and should stay, but Numbers
is too generic imho.
@@ -15,7 +15,7 @@ import { map, pipe, tap } from 'rxjs'; | |||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | |||
|
|||
@Component({ /* ... */ }) | |||
export class NumbersComponent { | |||
export class Numbers { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to this comment from Matthieu, maybe a more specific name?
https://x.com/Jean__Meche/status/1925866491506831784
export class Numbers { | |
export class NumbersDoubler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no recommendation in the official docs that components with a single word or generic name should be avoided.
https://next.angular.dev/style-guide#naming
This component is created just to show how rxMethod
should be used. It does not have a template that visualizes a specific use case, and because of that, I haven't named it NumberList
, NumbersCard
, NumberDetails
, or similar. In real-world scenarios, we would not create a component for logging double numbers anyway.
import { map, pipe, tap } from 'rxjs'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
|
||
@Component({ /* ... */ }) | ||
export class NumbersComponent implements OnInit { | ||
export class Numbers { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers { | |
export class NumbersDoubler { |
import { map, pipe, tap } from 'rxjs'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
|
||
@Component({ /* ... */ }) | ||
export class NumbersComponent implements OnInit { | ||
export class Numbers { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers { | |
export class NumbersDoubler { |
import { interval, map, of, pipe, tap } from 'rxjs'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
|
||
@Component({ /* ... */ }) | ||
export class NumbersComponent implements OnInit { | ||
export class Numbers { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers { | |
export class NumbersDoubler { |
@@ -221,7 +221,7 @@ export class NumbersService { | |||
} | |||
|
|||
@Component({ /* ... */ }) | |||
export class NumbersComponent implements OnInit { | |||
export class Numbers implements OnInit { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers implements OnInit { | |
export class NumbersDoubler implements OnInit { |
@@ -103,13 +103,13 @@ When a `signalMethod` is created in an ancestor injection context, it's necessar | |||
|
|||
```ts | |||
@Component({ /* ... */ }) | |||
export class NumbersComponent implements OnInit { | |||
export class Numbers implements OnInit { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers implements OnInit { | |
export class NumbersDoubler implements OnInit { |
readonly numbersService = inject(NumbersService); | ||
readonly injector = inject(Injector); | ||
|
||
ngOnInit(): void { | ||
const value = signal(1); | ||
// 👇 Providing the `NumbersComponent` injector | ||
// 👇 Providing the `Numbers` component injector |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 👇 Providing the `Numbers` component injector | |
// 👇 Providing the `NumbersDoubler` component injector |
@@ -127,10 +127,10 @@ The `signalMethod` must be initialized within an injection context. To initializ | |||
|
|||
```ts | |||
@Component({ /* ... */ }) | |||
export class NumbersComponent implements OnInit { | |||
export class Numbers implements OnInit { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers implements OnInit { | |
export class NumbersDoubler implements OnInit { |
@@ -145,7 +145,7 @@ At first sight, `signalMethod`, might be the same as `effect`: | |||
|
|||
```ts | |||
@Component({ /* ... */ }) | |||
export class NumbersComponent { | |||
export class Numbers { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export class Numbers { | |
export class NumbersDoubler { |
@@ -147,7 +147,7 @@ export class BooksComponent implements OnInit { | |||
) | |||
); | |||
|
|||
ngOnInit(): void { | |||
constructor() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven’t found anything in the documentation that explicitly recommends preferring the constructor over lifecycle hooks. The closest reference is this guideline about keeping lifecycle methods short: https://next.angular.dev/style-guide#keep-lifecycle-methods-simple
There are also some hooks which will get deprecated for Signal Components, but OnInit
is not among them: angular/angular#49682
That said, I personally default to using the constructor—unless I need to implicitly access an InputSignal. The main reason is that the constructor allows for early field initialization and any setup that needs to happen as soon as possible. It also runs within the injection context, which can be important for certain use cases.
Summary: I'm fine for moving to constructor, even if it is not listed in the new style guide.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Does this PR introduce a breaking change?