Reference's onInvalidated()
called after map.put(snapshot)
when snapshot doesn't have an id
#1874
Open
3 tasks done
Labels
bug
Confirmed bug
help/PR welcome
Help/Pull request from contributors to fix the issue is welcome
level: intermediate
Bug report
Sandbox link or minimal reproduction code
https://codesandbox.io/s/mobx-state-tree-issue-1874-gl01j
I've just started using MobX and MST to replace (a very poor implementation of) Redux in my app, and I came across some behaviour that seems like a bug. The closest related discussions I've found are here and maybe here.
The linked codesandbox has a similar structure to part of my app. It has Users and Todos. Todos have an array of assignedUsers safeReferences, and a primaryAssignedUser reference (this reference will also exist in the array). The array will never be empty, and primaryAssignedUser will always exist.
Models:
index.js:
Describe the expected behavior
Regardless of whether an id is provided in the snapshot,
onInvalidated()
should not be called.Describe the observed behavior
As indicated in the comments above (about LINE_A/LINE_B), everything works fine if the provided Todo snapshot already has an id. However, if I want to omit the id (since it's optional in the model):
onInvalidated()
gets called for each reference.safeReference
s will be undefined/removed.If I wasn't using
onInvalidated()
(manually or viasafeReference
), I wouldn't even notice the extra steps happening when omitting the id. And in my case, the ids are allcrypto.randomUUID()
, so working around this is just the slight inconvenience of manually adding the ids. This tripped me up for a while though, and I'm not sure whether it's a bug, or undocumented but expected behaviour – similar to how callingmap.put(Todo.create({...}))
would understandably result in invalid references, since atTodo.create({...})
the model instance is not yet part of the tree.The text was updated successfully, but these errors were encountered: