Skip to content

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

Merged
merged 2 commits into from
May 28, 2025

Conversation

markostanimirovic
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[x] Documentation content changes
[ ] Other... Please describe:

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Copy link

netlify bot commented May 21, 2025

Deploy Preview for ngrx-io ready!

Name Link
🔨 Latest commit a1b6a1c
🔍 Latest deploy log https://app.netlify.com/projects/ngrx-io/deploys/6836c6887e7ec700088b47d5
😎 Deploy Preview https://deploy-preview-4790--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented May 21, 2025

Deploy Preview for ngrx-site-v19 ready!

Name Link
🔨 Latest commit a1b6a1c
🔍 Latest deploy log https://app.netlify.com/projects/ngrx-site-v19/deploys/6836c688cc5915000850d32d
😎 Deploy Preview https://deploy-preview-4790--ngrx-site-v19.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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() {
Copy link
Member

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)

Copy link
Contributor

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.

Copy link
Member Author

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?

@timdeschryver

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.

Copy link
Member

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 👍

Copy link
Contributor

@rainerhahnekamp rainerhahnekamp left a 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 {
Copy link
Contributor

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

Suggested change
export class Numbers {
export class NumbersDoubler {

Copy link
Member Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export class Numbers {
export class NumbersDoubler {

@@ -221,7 +221,7 @@ export class NumbersService {
}

@Component({ /* ... */ })
export class NumbersComponent implements OnInit {
export class Numbers implements OnInit {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 👇 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export class Numbers {
export class NumbersDoubler {

@@ -147,7 +147,7 @@ export class BooksComponent implements OnInit {
)
);

ngOnInit(): void {
constructor() {
Copy link
Contributor

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.

@markostanimirovic markostanimirovic merged commit 2af5f10 into main May 28, 2025
14 checks passed
@markostanimirovic markostanimirovic deleted the docs/signals/naming-convention branch May 28, 2025 08:24
rainerhahnekamp pushed a commit to rainerhahnekamp/ngrx that referenced this pull request Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants