Skip to content

References (safe or not) and onInvalidated #2188

Answered by coolsoftwaretyler
cavasinf asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @cavasinf - this is a subtle issue. Very reasonable to get tripped up by it (it took me a few minutes to debug it haha).

In your addTodo action, you're setting the reference to the User instance in a newly created Todo. But that Todo is not yet part of the tree with the reference available.

So when it's instantiated, MobX-State-Tree says "hmm, I don't know about a user with the ID of 3, this is invalid".

This is not the case in your original store creation call, since todos exists in the same tree as users when Todo of ID 1 is instantiated.

Here is a code sandbox that no longer warns the invalidation: https://codesandbox.io/p/sandbox/mobx-state-tree-getting-started-13-forked-23jcls?fil…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@cavasinf
Comment options

@coolsoftwaretyler
Comment options

Comment options

You must be logged in to vote
2 replies
@cavasinf
Comment options

@coolsoftwaretyler
Comment options

Answer selected by coolsoftwaretyler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants