Preserve equal selected row references across later live-query publications #2320
Versions#
@tanstack/db0.15.2@tanstack/react-db0.5.12- React 19.3.1
Observable behavior#
A live query suppresses notification when an update does not change its selected value, but still replaces the stored selected row object. A later update to another row exposes that replacement to useLiveQuery. Consequently React.memo row components rerender for unchanged selected values. An order-only update also replaces the selected row objects.
This occurs with both declarative .select and .fn.select.
Minimal projection#
import {
createCollection,
createLiveQueryCollection,
localOnlyCollectionOptions,
useLiveQuery,
} from '@tanstack/react-db'
import { memo } from 'react'
type Item = { id: string; title: string; revision: number; position: number }
const items = createCollection(localOnlyCollectionOptions<Item>({
id: 'items',
getKey: row => row.id,
initialData: [
{ id: 'p1', title: 'one', revision: 0, position: 1 },
{ id: 'p2', title: 'two', revision: 0, position: 2 },
],
}))
const rows = createLiveQueryCollection({
query: q => q.from({ item: items })
.orderBy(({ item }) => item.position)
.select(({ item }) => ({ id: item.id, title: item.title })),
getKey: row => row.id,
})
const Row = memo(({ row }: { row: { id: string; title: string } }) => {
console.count(`render ${row.id}`)
return <div>{row.title}</div>
})
export function List() {
const { data } = useLiveQuery(rows)
return data.map(row => <Row key={row.id} row={row} />)
}
After mounting and preloading, execute each mutation separately, allowing React to settle between steps:
// 1. Changes only a field excluded from the projection.
await items.update('p1', row => { row.revision++ }).when('settled')
// 2. Changes only the other row's selected value.
await items.update('p2', row => { row.title = 'two changed' }).when('settled')
// 3. Reorders rows without changing either projected value.
await items.update(['p1', 'p2'], drafts => {
for (const row of drafts) row.position = row.id === 'p1' ? 2 : 1
}).when('settled')
Measured result#
A React/DOM harness using the public APIs above, without StrictMode, produced:
| Step | p1 renders | p2 renders | Detail |
|---|---|---|---|
| Initial | 1 | 1 | Order p1, p2 |
| Revision-only p1 update | 1 | 1 | rows.get('p1') already differs by reference from its initial value |
| p2 title update | 2 | 2 | p1 receives a new reference despite identical selected fields |
| Position-only swap | 3 | 3 | Order correctly becomes p2, p1; both row references change |
Requested behavior#
Preserve a selected row's reference when its final selected value is equal, including across later publications and order-only changes. The result array/order can change while its unchanged row objects remain stable.
The current public snapshot documentation promises stable snapshot identity per revision; it does not explicitly promise this stronger row-level structural sharing. If retaining equal row references is intentionally outside the current contract, please treat this as a request to support it or document a supported way to obtain it.
Impact and implementation consideration#
Memoized row components and computations keyed by row objects perform extra work for unchanged displayed data. The delayed identity change is especially surprising because the originating update emitted no value-change event. Collection publication already compares values for event suppression; retaining the prior value at that boundary may be preferable to another equality cache in each consumer. Any implementation must preserve layout notifications and actual value updates.
Related work#
- #1829 was fixed in DB 0.8.1/react-db 0.3.1. That issue concerned subscriber rerenders on unrelated source revisions. Here the equal-only update does not rerender; reference churn becomes visible on the next relevant publication.
- #1601 and #1623 cover order-only publication and the shared observer/layout contract. This report concerns selected row references while preserving that publication behavior.
TkDodo closed this
Sign in to join this conversation.