in the store.ts
get value() {
return this.subject.value;
}
if the component mutates the value received via this property (for example via a developer mistake), we actually mutate our store !
this is because your store's data is kept in the behaviorSubject .
isnt it better to keep an additional separate private property that will hold our store data.
and in the
set(name: string, state: any) {
this.subject.next({
...this.value, [name]: state
});
}
instead of using 'this.value' (which could have been mutated), we use that private property (and also update it during this process, to have the latest store data).
in the store.ts
get value() {
return this.subject.value;
}
if the component mutates the value received via this property (for example via a developer mistake), we actually mutate our store !
this is because your store's data is kept in the behaviorSubject .
isnt it better to keep an additional separate private property that will hold our store data.
and in the
set(name: string, state: any) {
this.subject.next({
...this.value, [name]: state
});
}
instead of using 'this.value' (which could have been mutated), we use that private property (and also update it during this process, to have the latest store data).