You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/mobx-race-condition-pattern.mdx
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,6 +89,43 @@ Follow these rules to avoid this race condition:
89
89
Create a linting rule or code review checklist item to catch stored references before `await` statements in MobX store methods.
90
90
</Callout>
91
91
92
+
## MobX-State-Tree Connection
93
+
94
+
If you're using MobX-State-Tree, you may encounter this specific error message:
95
+
96
+
<Callouttype="danger"title="MST Error">
97
+
"This object has died and is no longer part of a state tree. It cannot be used anymore."
98
+
</Callout>
99
+
100
+
This is the same underlying issue — the object reference was detached when the tree was updated. MST is more explicit about this problem because it tracks object lifecycle, while vanilla MobX will silently allow operations on detached objects.
101
+
102
+
## Alternative: Using `flow` Generators
103
+
104
+
MobX's `flow` function with generators can help structure async code more cleanly, though it **doesn't prevent** the stale reference issue when objects are recreated:
The `flow` pattern is useful for automatically wrapping state updates in actions, but the core principle remains: **always re-fetch object references after any yield/await**.
Understanding this pattern will save you from mysterious bugs where state updates seem to disappear into thin air. Always remember: in MobX with async operations, **object references are temporary** — re-fetch after every await.
0 commit comments