Skip to content

🐞[BUG]:Mutating selector data unexpectedly modifies the state without dispatching an action #2313

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
lgorecki opened this issue Mar 20, 2025 · 8 comments

Comments

@lgorecki
Copy link

lgorecki commented Mar 20, 2025

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


Libs:
- @angular/core version: 18.2.13
- @ngxs/store version: 18.1.6
@lgorecki lgorecki changed the title 🐞[BUG]: 🐞[BUG]:Mutating selector data unexpectedly modifies the state without dispatching an action Mar 20, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
@ngxs ngxs deleted a comment May 10, 2025
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

No branches or pull requests

1 participant