How to assign a ref-type object to a nested observable property at runtime? #4583
-
|
I'm using MobX v6+ and trying to assign a ref-type object to a nested property inside an observable structure. Here's the setup: By default, foo.bar.gar becomes deeply observable. But I want to replace gar with a plain object and have MobX treat it as a reference, so that only reassignment of gar triggers reactions—not mutations inside it. I tried: I understand that observable.ref is meant to be used as an annotation, but I need to apply it at runtime, not during object construction or inside makeObservable. In short:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
If you don't know upfront which property should not become observable, you cannot annotate indeed. However, MobX will never make any class instance automatically observable, so you can use that to prevent auto observing. E.g. something like this will do the trick: class Ref<T> {
constructor(private value: T) {}
get(): T { return this.value }
}Now you can do something like If the object |
Beta Was this translation helpful? Give feedback.
Or alternatively
foo.bar.gar = makeObservable({ zar: 456 }, { /* no properties annotated */ }, {proxy:false /* don't wrap in a Proxy */})would likely do the trick as well, it marks the object as 'observable', but none of the properties actually will be.