The problem with using RxJS or Signals for global state management
While our current state management solution is used in many applications and works well for our current application, there’s a huge problem: our current global state management solution isn’t immutable.
You cannot modify your BehaviorSubject
classes or Signals from outside the store, so in that sense, it is immutable. Also, when using primitive values for your state, the state itself is immutable. However, when you’re using reference objects as values for your BehaviorSubject
classes or Signals, the state itself isn’t immutable.
When you use an array or object for your state and retrieve the state through BehaviorSubject
or Signal
, you can modify the value of the state unintentionally. When you adjust the retrieved state object within a component or service class, the value of BehaviorSubject
or Signal
is also modified!
This is also the reason why we used the structuredClone()
function...