mobx-keystone
mobx-keystone copied to clipboard
How to destroy a reference?
It seems that just creating a new reference to a tree node is enough for getRefsResolvingTo()
to return a Set with one Ref pointing to this tree node. My question is how can we destroy this ref?
In the Todo List example from the documentation about References, the reference to the selected Todo is somehow destroyed by setting selectedRef
to undefined
. But how to do that if the created Ref is not attached to a prop<Ref<Todo>>
of an other model? It is possible to destroy directly the Ref itself?
you mean a root ref or a custom one? if custom, how does it work?
It is a custom ref that uses a context in resolve()
to resolve the reference to the target model. This latter is in model store (a simple list of models).
In theory, as soon as the custom reference resolve function stops returning a resolved model (returns undefined) it should automatically be removed from the getRefsResolvingTo list.
Maybe you could add a blacklist in the context, make the custom ref return undefined when it is blacklisted and push something to that context blacklist when you want a ref to get "invalidated"?
Thank you for the explanations and the suggestion.
as soon as the custom reference resolve function stops returning a resolved model (returns undefined) it should automatically be removed from the getRefsResolvingTo list.
This is the important point to know.