Skip to content

docs(signals): mention re-use of withComputed/withMethods #4780

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions projects/ngrx.io/content/guide/signals/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,29 @@ export class Counter {
}
```
</details>

<details>
<summary><b>#6</b> Can features like `withComputed` or `withMethods` reference other members inside the same feature?</summary>

It may be necessary for a computed in a `withComputed` feature to need to reference another computed value,
or a method in a `withMethods` feature to refer to another method. To do so, you can break out the common piece
with a helper `const` that can serve as a function or computed itself.

Although it is possible to have multiple features that reference each other, we recommend having everything in one call.
That adheres more to JavaScript's functional style and keeps features co-located.

```ts
export const BooksStore = signalStore(
withState(initialState),
withComputed(({ filter }) => {
// 👇 Define helper functions (or computeds).
const sortDirection = computed(() => (filter.order() === 'asc' ? 1 : -1));

return {
sortDirection: sortDirection,
sortDirectionReversed: computed(() => sortDirection() * -1),
};
})
);
```
</details>