Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useLiveQuery losing items reference #2034

Open
mpirescarvalho opened this issue Jul 11, 2024 · 0 comments
Open

useLiveQuery losing items reference #2034

mpirescarvalho opened this issue Jul 11, 2024 · 0 comments

Comments

@mpirescarvalho
Copy link

When using Dexie's immutable cache and useLiveQuery, unchanged items do not retain their references under certain conditions, specifically when using .reverse() or when updating a single item within a transaction.

  • Using .reverse():
const db = new Dexie("database", {
  cache: "immutable",
});

// Always gives a new reference
const items = useLiveQuery(async () => {
  const items = await db.items2.reverse().toArray();
  return items;
});

// Keeps references for unchanged items
const items = useLiveQuery(async () => {
  const items = await db.items.toArray();
  return [].concat(items).reverse();
});

// Later...
await db.items2.update(item.id, { isActive: !item.isActive });

In the first example, using .reverse() directly on the query results in new references being created, even if the items themselves haven't changed. Conversely, reversing an array after retrieval preserves the references for unchanged items.

  • Using Transactions:
// useLiveQuery will refresh references even without using .reverse()
await db.transaction("rw", item2 ? db.items2 : db.items, async () => {
  await db.items.update(item.id, { isActive: !item.isActive });
});
When updating a single item within a transaction, references are lost, resulting in new references for all items in the query, despite only one item being updated.

Expected Behavior:

Unchanged items should retain their references to avoid unecessary rerenders.

Reproducible Example: (open console)
CodeSandbox link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant