Skip to content

docs(ngrx/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 1 commit into
base: main
Choose a base branch
from

Conversation

michael-small
Copy link

Issue: #4669

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[x] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

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

Closes #4669

What is the new behavior?

Documentation mentions how to re-use a computed or function across or within a withComputed or withMethods

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Copy link

netlify bot commented May 13, 2025

Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 08d275d
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/6823db369f34870008536d55
😎 Deploy Preview https://deploy-preview-4780--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented May 13, 2025

Deploy Preview for ngrx-site-v19 failed.

Name Link
🔨 Latest commit 08d275d
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-site-v19/deploys/6823db36daf17b0008213457

@michael-small
Copy link
Author

@timdeschryver @rainerhahnekamp since you two coached me in the issue

I tweaked the example code comments a little.

I added the block towards the end of the "Core Concepts > Defining Store Methods" as this pertains to both withMethods and withComputed which by then are both explained.

Comment on lines +301 to +306
withComputed(({ books, filter }) => ({
booksCount: computed(() => books().length),
sortDirection: computed(() => filter.order() === 'asc' ? 1 : -1),
})),
// 👇 (A) Also access previously defined computed properties (or functions).
withComputed(({ books, sortDirection }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was writing this page, I assumed that readers are familiar with core JavaScript concepts, so they're aware that () => ({}) is a sugar syntax for () => { return {} }.

I'm not a fan of using multiple withComputed calls. The native JS way should be preferred and recommended in my opinion:

withComputed({ books, filter }) => {
  const booksCount = computed(() => books().length);
  const sortDirection = computed(() => /* ... */);
  // ...
  
  return { /* export public computed signals */ };
}),

Additionally, it provides the ability not to export private computed signals if necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was writing this page, I assumed that readers are familiar with core JavaScript concepts, so they're aware that () => ({}) is a sugar syntax for () => { return {} }.

(sorry if this is overselling but I have thought on this a lot) In retrospect that made sense when I learned this after seeking it out, but I have seen people with this same problem frequently across different platforms. It has even been news to some GDEs who make signal store content. I can't speak for why the various other people didn't recognize this sugar syntax, but for me I just think going from a class based store to a function based one has a lot of little moments like this. And this point for people who wonder about this seems to solve a large hurdle with a small tip.

I'm not a fan of using multiple withComputed calls. The native JS way should be preferred and recommended in my opinion:

I agree. That said, in the places this has come up, it feels like 2/3 of people are like "great, no need for another withComputed" and the other 1/3 are like "that's cool that the const method works good but I prefer multiple withComputed". So in my opinion, I feel like this const helper restructuring is worth a tip somewhere and that people who prefer multiple withComputed are just going to keep doing that lol.

Copy link
Author

@michael-small michael-small May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having this PR as still adding this tip with respect to the native const approach, but dropping the repeated withComputed/withMethods reference?

@timdeschryver / @rainerhahnekamp you both suggested having them both, so I'm curious if my proposed compromise which tentatively drop the repeated withComputed sounds legit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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