Skip to content

Commit 3c4b66e

Browse files
michael-smallMarko Stanimirović
andauthored
docs(signals): mention re-use of withComputed/withMethods (#4780)
Closes #4669 Co-authored-by: Marko Stanimirović <[email protected]>
1 parent c8e1352 commit 3c4b66e

File tree

2 files changed

+60
-5
lines changed
  • projects
    • ngrx.io/content/guide/signals
    • www/src/app/pages/guide/signals

2 files changed

+60
-5
lines changed

projects/ngrx.io/content/guide/signals/faq.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,29 @@ export class Counter {
7575
}
7676
```
7777
</details>
78+
79+
<details>
80+
<summary><b>#6</b> Can features like `withComputed` or `withMethods` reference other members inside the same feature?</summary>
81+
82+
It may be necessary for a computed in a `withComputed` feature to need to reference another computed value,
83+
or a method in a `withMethods` feature to refer to another method. To do so, you can break out the common piece
84+
with a helper that can serve as a function or computed itself.
85+
86+
Although it is possible to have multiple features that reference each other, we recommend having everything in one call.
87+
That adheres more to JavaScript's functional style and keeps features co-located.
88+
89+
```ts
90+
export const BooksStore = signalStore(
91+
withState(initialState),
92+
withComputed(({ filter }) => {
93+
// 👇 Define helper functions (or computed signals).
94+
const sortDirection = computed(() => (filter.order() === 'asc' ? 1 : -1));
95+
96+
return {
97+
sortDirection,
98+
sortDirectionReversed: () => sortDirection() * -1,
99+
};
100+
})
101+
);
102+
```
103+
</details>

projects/www/src/app/pages/guide/signals/faq.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Frequently Asked Questions
22

33
<details>
4-
<summary>How to connect my SignalStore(s) with Redux DevTools?</summary>
4+
<summary><b>#1</b> How to connect my SignalStore(s) with Redux DevTools?</summary>
55

66
There's no official connection between `@ngrx/signals` and the Redux Devtools.
77
We expect the Angular Devtools will provide support for signals soon, which can be used to track the state.
@@ -10,7 +10,7 @@ However, you could create a feature for this, or you can make use of the [`withD
1010
</details>
1111

1212
<details>
13-
<summary>Can I use the Flux/Redux pattern with SignalStore?</summary>
13+
<summary><b>#2</b> Can I use the Flux/Redux pattern with SignalStore?</summary>
1414

1515
Yes. Starting from NgRx version 19.2, the Events plugin introduces support for a Flux-style state management with SignalStore.
1616
It enables defining and dispatching events, handling them through reducers and effects, and maintaining a unidirectional data flow similar to the traditional Redux pattern.
@@ -19,7 +19,7 @@ For more information, see the Events Plugin documentation.
1919
</details>
2020

2121
<details>
22-
<summary>Can I define my SignalStore as a class?</summary>
22+
<summary><b>#3</b> Can I define my SignalStore as a class?</summary>
2323

2424
Yes, it is possible to define a SignalStore using a class-based approach.
2525
However, the NgRx team recommends using the functional style for defining SignalStores.
@@ -43,7 +43,7 @@ export class CounterStore extends signalStore(
4343
</details>
4444

4545
<details>
46-
<summary>How can I get the type of a SignalStore?</summary>
46+
<summary><b>#4</b> How can I get the type of a SignalStore?</summary>
4747

4848
To get the type of a SignalStore, use the `InstanceType` utility type.
4949

@@ -60,7 +60,7 @@ function logCount(store: CounterStore): void {
6060
</details>
6161

6262
<details>
63-
<summary>Can I inject a SignalStore via the constructor?</summary>
63+
<summary><b>#5</b> Can I inject a SignalStore via the constructor?</summary>
6464

6565
Yes. To inject a SignalStore via the constructor, define and export its type with the same name.
6666

@@ -82,3 +82,32 @@ export class Counter {
8282
```
8383

8484
</details>
85+
86+
<details>
87+
<summary><b>#6</b> Can features like `withComputed` or `withMethods` reference other members inside the same feature?</summary>
88+
89+
It may be necessary for a computed in a `withComputed` feature to need to reference another computed value,
90+
or a method in a `withMethods` feature to refer to another method. To do so, you can break out the common piece
91+
with a helper that can serve as a function or computed itself.
92+
93+
Although it is possible to have multiple features that reference each other, we recommend having everything in one call.
94+
That adheres more to JavaScript's functional style and keeps features co-located.
95+
96+
```ts
97+
export const BooksStore = signalStore(
98+
withState(initialState),
99+
withComputed(({ filter }) => {
100+
// 👇 Define helper functions (or computed signals).
101+
const sortDirection = computed(() =>
102+
filter.order() === 'asc' ? 1 : -1
103+
);
104+
105+
return {
106+
sortDirection,
107+
sortDirectionReversed: () => sortDirection() * -1,
108+
};
109+
})
110+
);
111+
```
112+
113+
</details>

0 commit comments

Comments
 (0)