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
Mutating selector data unexpectedly modifies the state without dispatching an action.
I encountered an issue where data retrieved from a selector was unintentionally modified by an Angular form control, leading to unintended state changes. My assumption was that selectors should return immutable data, preventing direct state modifications. However, this behavior suggests that the selector's return value is mutable, causing state mutations without explicit actions.
Found similar issue on StackOverflow from 4 years ago where the answer claims that it should at least complain in devmode that it is readonly but there are no such notifications. https://stackoverflow.com/questions/66563574/angular-mutating-ngxs-selector-data-is-modifying-state-without-an-action
Best case scenario I believe that NGXS selectors should be immutable or at least readonly so we can get warning that we are modifying readonly data. If the official statement is that developers should decide when to make selector immutable please share some guidelines on this matter because from my experience it destroys any optimization for selectors done in the library and makes them run much more often than they should.
(even better to check on own app with redux tools)
Note: The provided example demonstrates a bad pattern but illustrates the issue I encountered. In my actual application, I retrieved data from a selector and passed it to an Angular form. Unexpectedly, one of the form controls modified an object inside array within the selector’s data, affecting the application state without an explicit action dispatch.
Uh oh!
There was an error while loading. Please reload this page.
Affected Package
@ngxs/store >= 18.1.5
Is this a regression?
Not sure
Description
Mutating selector data unexpectedly modifies the state without dispatching an action.
I encountered an issue where data retrieved from a selector was unintentionally modified by an Angular form control, leading to unintended state changes. My assumption was that selectors should return immutable data, preventing direct state modifications. However, this behavior suggests that the selector's return value is mutable, causing state mutations without explicit actions.
Found similar issue on StackOverflow from 4 years ago where the answer claims that it should at least complain in devmode that it is readonly but there are no such notifications. https://stackoverflow.com/questions/66563574/angular-mutating-ngxs-selector-data-is-modifying-state-without-an-action
Best case scenario I believe that NGXS selectors should be immutable or at least readonly so we can get warning that we are modifying readonly data. If the official statement is that developers should decide when to make selector immutable please share some guidelines on this matter because from my experience it destroys any optimization for selectors done in the library and makes them run much more often than they should.
🔬 Minimal Reproduction
A reproducible example is available here:
StackBlitz Demo
(even better to check on own app with redux tools)
Note: The provided example demonstrates a bad pattern but illustrates the issue I encountered. In my actual application, I retrieved data from a selector and passed it to an Angular form. Unexpectedly, one of the form controls modified an object inside array within the selector’s data, affecting the application state without an explicit action dispatch.
🔥 Exception or Error
No error
Environment
The text was updated successfully, but these errors were encountered: