-
-
Notifications
You must be signed in to change notification settings - Fork 2k
docs(@ngrx/signals
): add a mention of restructuring withComputed
or withMethods
for re-use in those features
#4669
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
Comments
withComputed
or withMethods
for re-use in those features@ngrx/signals
): add a mention of restructuring withComputed
or withMethods
for re-use in those features
@michael-small, I think both approaches work fine. Sometimes, I'd like to point out that there are "foundational" methods or computeds. In that case, two features are exactly what I need. Maybe add both options to the FAQ? |
Yeah, good point. Both are valid, and I imagine one may come to people more naturally than the other. But it is important to know they both exist for whatever approach the user may need., the foundational blocks, or the self contained. |
I think the first approach is more obvious and natural, and people already "know" this because its behavior is mentioned in the docs IIRC. The second approach can be a useful "tip" that isn't very obvious. |
That makes sense. So essentially what you are saying if I understand right, @timdeschryver , it would be some tip like that in one of the non-FAQ pages (possibly "Core Concepts" ??), and wrapped in one of those @rainerhahnekamp sounds legit? |
Yep, that's exactly what I mean @michael-small |
Yup @michael-small |
@timdeschryver @rainerhahnekamp Thoughts on this being the example (short of maybe comments inline)? withComputed(({ books, filter }) => {
const direction = computed(() => (filter.order() === 'asc' ? 1 : -1));
const sortBooks = (direction: number) =>
books().toSorted((a, b) => direction * a.title.localeCompare(b.title));
return {
sortedBooks: computed(() => sortBooks(direction())),
reverseSortedBooks: computed(() => sortBooks(-1 * direction())),
};
}), Benefits
|
@michael-small sorry for the late reply. With seeing the example, I think we can combine both approaches in the same snippet. The existing snippet can be changed to: export const BooksStore = signalStore(
withState(initialState),
// 👇 Accessing previously defined state signals and properties.
withComputed(({ books, filter }) => ({
booksCount: computed(() => books().length),
sortDirection: computed(() => filter.order() === 'asc' ? 1 : -1),
})),
// 👇 Also access previously defined computed properties.
withComputed(({ books, sortDirection }) => {
// 👇 Define helper functions.
const sortBooks = (direction: number) =>
books().toSorted((a, b) => direction * a.title.localeCompare(b.title));
return {
sortedBooks: computed(() => sortBooks(sortDirection())),
reversedBooks: computed(() => sortBooks(-1 * sortDirection())),
};
}),
); |
@timdeschryver thank you. I'll think of a good spot to drop it in on the page and make a PR. |
Information
The first major stumbling point I had with using features like
withComputed
orwithMethods
was the need to reference other computeds or methods in those features. For example, if I wanted awithComputed
to expose be able to return afullName
that was a store'sfirstName
and store'slastName
, and then also expose aallCapsFullName
that was the full name computed inside another computed. But do to what I thought was a limitation of how to return an object likewithComputed(({ firstName, lastName }) => ({...})
, I had to make a secondwithComputed
However, someone pointed out that the first
withComputed
could be restructured to have a return block, with helperconst
s or functions.In retrospect this is fairly obvious, but I see the former approach all the time and was stuck in that mindset myself. I think partially because most examples of things like
withComputed
have those returns without areturn
block, and because there is a lot of little syntax nuances with functions and objects in the signal store to become comfortable with that add up.I think something like an FAQ page point on this is warranted with how often this little nuance turns into what is perceived as a bigger problem than it is with an easy solution. Or some examples of the second syntax baked into other pages. Willing to formalize/condense this language to make a PR.
Documentation page
https://ngrx.io/guide/signals/faq
I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: