Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/wet-hairs-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/store': patch
---

Fix a regression where mutable atoms could be updated internally with no updater and have their snapshot replaced with `undefined`.

Mutable atoms now ignore internal no-argument `_update()` calls, while computed atoms keep existing recomputation behavior. This prevents external-store state from disappearing during reactive graph cleanup.
12 changes: 9 additions & 3 deletions packages/store/src/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ export function createAtom<T>(
_update(getValue?: T | ((snapshot: T) => T)): boolean {
const prevSub = activeSub
const compare = options?.compare ?? Object.is
activeSub = atom
++cycle
atom.depsTail = undefined
if (isComputed) {
activeSub = atom
++cycle
atom.depsTail = undefined
} else if (getValue === undefined) {
// Mutable atoms can be marked dirty by the reactive graph, but they should
// never be recomputed without an explicit value/updater.
return false
}
if (isComputed) {
atom.flags = ReactiveFlags.Mutable | ReactiveFlags.RecursedCheck
}
Expand Down
8 changes: 8 additions & 0 deletions packages/store/tests/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ describe('store', () => {
store.setState(() => 24)
expect(fn).toBeCalledWith({ prevVal: 12, currentVal: 24 })
})

test('mutable store should ignore internal no-arg updates', () => {
const store = createStore({ pageIndex: 1 })
const atom = (store as any).atom

expect(atom._update()).toBe(false)
expect(store.state).toEqual({ pageIndex: 1 })
})
})
Loading