You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to select arbitrary slices of state efficiently without having to manually create lots of individual selectors.
Describe the problem you are trying to solve
Please describe the problem. If possible please substantiate it with the use cases you want to address.
It is common to need to select a handful of properties (and use them directly) from a state store in one selector. For example, I have an AppSettings model that contains various properties used throughout my app. My initial naive implementation of such a selector looked like this:
The problem with this implementation was that, if any property in AppSettings changes, regardless of whether or not it is listed the above selector, all consumers of this selector will recalculate unless they are guarded (e.g., with a distinctUntilChanged((x,y) => ...). I don't believe this is the right solution.
Currently to solve this problem, it appears that individual selectors are needed, which can then be aggregated into a composite selector. I broke apart my settings into different selectors as follows, which solves the problem of downstream recalculations:
The above works, but feels overly verbose, especially if you have multiple configurations you'd like across the app for various state slices. I would love to have a simpler alternative that can pick out various arbitrary properties from a state store as needed.
See this post/thread on the NGXS slack for the discussion sparking this post.
Describe the solution you'd like
If you have a solution in mind, please describe it.
I would like a createSelector helper method of some kind that can provide a strongly typed solution to the above. I have come up with the following implementation in my own app and it currently appears to work well; I've been able to remove quite a few "boilerplate" selectors so far with this:
The above implementation both provides strong typing for consumers and solves the problem of recalculating unnecessarily when unrelated state changes occur. I hadn't personally had a reason to move over to using StateTokens until this and so I'm not sure of how well adopted they are - which could be a potential drawback for this solution - but it was trivial to add one and well worth it for the result.
Describe alternatives you've considered
Have you considered any alternative solutions or workarounds?
I'm using what I posted above in the suggested solution. I would be happy to put in a PR to add this to the library if desired. And by the way, thanks for the great library!
Great news! v3.8.0 has been released and it includes a fix for this issue.
We are closing this issue, but please re-open it if the issue is not resolved.
Please leave a comment in the v3.8.0 release discussion if you come across any regressions with respect to your issue.
Relevant Package
This feature request is for @ngxs/storeDescription
I would like to be able to select arbitrary slices of state efficiently without having to manually create lots of individual selectors.
Describe the problem you are trying to solve
Please describe the problem. If possible please substantiate it with the use cases you want to address.It is common to need to select a handful of properties (and use them directly) from a state store in one selector. For example, I have an
AppSettings
model that contains various properties used throughout my app. My initial naive implementation of such a selector looked like this:The problem with this implementation was that, if any property in
AppSettings
changes, regardless of whether or not it is listed the above selector, all consumers of this selector will recalculate unless they are guarded (e.g., with adistinctUntilChanged((x,y) => ...)
. I don't believe this is the right solution.Currently to solve this problem, it appears that individual selectors are needed, which can then be aggregated into a composite selector. I broke apart my settings into different selectors as follows, which solves the problem of downstream recalculations:
The above works, but feels overly verbose, especially if you have multiple configurations you'd like across the app for various state slices. I would love to have a simpler alternative that can pick out various arbitrary properties from a state store as needed.
See this post/thread on the NGXS slack for the discussion sparking this post.
Describe the solution you'd like
If you have a solution in mind, please describe it.I would like a
createSelector
helper method of some kind that can provide a strongly typed solution to the above. I have come up with the following implementation in my own app and it currently appears to work well; I've been able to remove quite a few "boilerplate" selectors so far with this:The above implementation both provides strong typing for consumers and solves the problem of recalculating unnecessarily when unrelated state changes occur. I hadn't personally had a reason to move over to using
StateToken
s until this and so I'm not sure of how well adopted they are - which could be a potential drawback for this solution - but it was trivial to add one and well worth it for the result.Describe alternatives you've considered
Have you considered any alternative solutions or workarounds?I'm using what I posted above in the suggested solution. I would be happy to put in a PR to add this to the library if desired. And by the way, thanks for the great library!
Edit - seems like this may be related to #1653
The text was updated successfully, but these errors were encountered: