The store
has a method called getState
that will return the current state of the store.
What prevents code somewhere in my application from (accidentally) modifying the returned state
from store
?
Let's say i call this:
let state = store.getState();
state.someProperty = 'fun';
The implementation that i've found on getState
on the store
object simply returns the inner state object that gets overwritten with each new action.
const getState = () => state;
In between actions/new states what prevents code from modifying the state that will be read by another subscriber? In my above example, setting someProperty
to 'fun'
will persist inside the store
on the state
property, until overwritten.
While i'm obviously not supposed to modify the state, a simple mistake might bind the state to some component that (unknowingly) modifies its inputs - perhaps on a 2-way binding in an angular environment?
<app-some-component [user]="state"></app-some-component>
Shouldn't getState()
be implemented as a clone of its state
model?
P.S. This is not specifically related to Angular - which is why i didn't add the tag - to allow more people not used to Angular to answer the question.