TanStackT
TanStack6mo ago
29 replies
sophisticated-purple

collection insert rollback instantly

perhaps doing it wrong but when i click on insert the data do
[{"id": 1}]
[{"id": 1}, {"id": 2}]
[{"id": 1}]

and theres no refetch

import { queryCollectionOptions } from "@tanstack/query-db-collection";
import { createCollection, useLiveQuery } from "@tanstack/react-db";
import { QueryClient } from "@tanstack/react-query";
import { Pressable, Text, View } from "react-native";

const queryClient = new QueryClient()

const todoCollection = createCollection(
  queryCollectionOptions({
    queryClient: queryClient,
    queryKey: ['todos'],
    queryFn: async () => {
      console.log("refetch")
      return [{ id: 1 }]
    },
    getKey: (item) => item.id,
    onInsert: async () => {
      return { refetch: false }
    }
  })
)

export default function Index() {
  const { data: todos } = useLiveQuery((q) =>
    q.from({ todo: todoCollection })
  )

  console.log(todos)

  return (
    <View
      style={{
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
      }}
    >
      <Pressable onPress={() => todoCollection.insert({ id: 2 })}><Text>insert</Text></Pressable>
    </View>
  );
}
Was this page helpful?