Description
Information
The first major stumbling point I had with using features like withComputed
or withMethods
was the need to reference other computeds or methods in those features. For example, if I wanted a withComputed
to expose be able to return a fullName
that was a store's firstName
and store's lastName
, and then also expose a allCapsFullName
that was the full name computed inside another computed. But do to what I thought was a limitation of how to return an object like withComputed(({ firstName, lastName }) => ({...})
, I had to make a second withComputed
withComputed(({ firstName, lastName }) => ({
fullName: computed(() => {
return `${firstName()} ${lastName()}`;
}),
})),
withComputed(({ fullName }) => ({
allCapsFullName: computed(() => {
return fullName().toUpperCase();
}),
})),
However, someone pointed out that the first withComputed
could be restructured to have a return block, with helper const
s or functions.
withComputed(({ firstName, lastName }) => {
const fullName = computed(() => {
return `${firstName()} ${lastName()}`;
})
return {
fullName: fullName,
allCapsFullName: computed(() => {
return fullName().toUpperCase();
}),
}
}),
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 a return
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
- Yes
- No