I am using NGXS as my state management library. I have a users state and a selector to get list of users. I also have a component where I want to update user's name. The way I want to do this is to copy the users list that I get from the selector observable and use ngModel with inputs.
Here is an example of what I am trying to achieve.
And a snipped of my state selector
@Selector()
static getAll(state: User[]) {
return [...state];
}
The problem is modifying the data received form the selector is modifying the actual state even without dispatching an Action. I understand its because I am mutating the objects that I get from the state directly but aren't selectors supposed to be readonly?
So the questions I have are
- I thought Selectors are supposed to be read only and not modify the state in any way. Are they not?
- Even changing the selector to
return state.map(user=>Object.assign({}, user))
doesnt work. Is is the correct way to define a Selector? - Or am I missing something entirely on how to use Selectors?