Skip to content

Commit 6776170

Browse files
author
Oleksandr Ratushnyi
committed
Enhance settings and expand MobX race condition article with additional sections on MobX-State-Tree and async handling
1 parent 414ca7c commit 6776170

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

.claude/settings.local.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ yarn-error.log*
3434
# typescript
3535
*.tsbuildinfo
3636
next-env.d.ts
37-
.env
37+
.env
38+
.claude/settings.local.json

src/content/blog/mobx-race-condition-pattern.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,43 @@ Follow these rules to avoid this race condition:
8989
Create a linting rule or code review checklist item to catch stored references before `await` statements in MobX store methods.
9090
</Callout>
9191

92+
## MobX-State-Tree Connection
93+
94+
If you're using MobX-State-Tree, you may encounter this specific error message:
95+
96+
<Callout type="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:
105+
106+
```typescript
107+
import { flow } from "mobx";
108+
109+
class MyStore {
110+
updateSomething = flow(function* (this: MyStore, id: string) {
111+
let obj = this.getObjectById(id);
112+
if (!obj) return;
113+
obj.setLoading(true);
114+
115+
const data = yield this.api.fetch(id);
116+
117+
// Still need to re-fetch after yield!
118+
obj = this.getObjectById(id);
119+
if (obj) {
120+
obj.updateData(data);
121+
obj.setLoading(false);
122+
}
123+
});
124+
}
125+
```
126+
127+
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**.
128+
92129
## Real-World Scenario
93130

94131
This bug commonly appears in applications with:
@@ -128,3 +165,11 @@ it('should handle concurrent object recreation during async operation', async ()
128165
---
129166

130167
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.
168+
169+
## Resources
170+
171+
<LinkCard href="https://mobx.js.org/actions.html" title="MobX Actions Documentation" />
172+
173+
<LinkCard href="https://github.com/mobxjs/mobx-state-tree/issues/416" title="MST Race Condition Issue #416" />
174+
175+
<LinkCard href="https://mobx.js.org/understanding-reactivity.html" title="Understanding MobX Reactivity" />

0 commit comments

Comments
 (0)