Skip to content
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

docs(@ngrx/signals): add a mention of restructuring withComputed or withMethods for re-use in those features #4669

Open
1 of 2 tasks
michael-small opened this issue Jan 10, 2025 · 0 comments

Comments

@michael-small
Copy link

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 consts 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
@michael-small michael-small changed the title docs: add a mention of restructuring withComputed or withMethods for re-use in those features docs(@ngrx/signals): add a mention of restructuring withComputed or withMethods for re-use in those features Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant