I am using angular 9 and ngrx 9
I have a selector that do like this :
this.store$
.pipe(select(SettingsStoreSelectors.selectNavigationSettings), take(1))
.subscribe((settings: SettingsStoreModels.INavigationSettings) => {
let copy = JSON.parse(JSON.stringify(settings));
this.settings = copy.vrMapSettings;
});
This is the only place settings is assigned in this component
In the component I then have a checkbox that edit a setting property
<mat-checkbox
class="setting-input"
*ngIf="viewer"
(click)="$event.stopPropagation()"
(change)="onSettingChange(settings, true)"
[(ngModel)]="settings.depthTestAgainstTerrain"
>
{{ "MAP.SETTINGS.DEPTH_TEST_AGAINST_TERRAIN" | translate }}
</mat-checkbox>
This keeps triggering
core.js:4117 ERROR TypeError: Cannot assign to read only property 'depthTestAgainstTerrain' of object '[object Object]'
But I don't understand anymore what to do. I deep cloned my store property, I want to edit to resubmit an action but it's not possible. why...
EDIT : actually I found out the reason why but I do not understand how to use it correctly then.
I deep clone the object like above from my selector
When a setting change, I dispatch an action to change the setting object in the store : this action setting object reference the same object that is used by the component.
this.store$.dispatch(new SettingsStoreAction.SetNavigationSettings({ settings: { vrMapSettings: settings } }));
Which call this reducer :
const SET_NAVIGATION_SETTINGS = (state: State, action: featureAction.SetNavigationSettings) => {
return {
...state,
navigation: {
...state.navigation,
...action.payload.settings,
},
};
};
But then, if I edit again, since the store reference directly the setting object of the component, it trigger the error....
If I deep clone here too
this.store$.dispatch(new SettingsStoreAction.SetNavigationSettings({ settings: { vrMapSettings: JSON.parse(JSON.stringify(settings))} }));
The it works...
But if I have to create small action for EVERY property of settings, or deep clone for EVERY dispatch/selection of the store, there is going to be a HUGE flaw in the performance...
am I using it correctly ?
if need more information please comment I will provide.