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

useFirestoreInfiniteQuery with realtime subscriptions supported? #131

Open
davidoort opened this issue Nov 25, 2024 · 1 comment
Open

useFirestoreInfiniteQuery with realtime subscriptions supported? #131

davidoort opened this issue Nov 25, 2024 · 1 comment

Comments

@davidoort
Copy link

Hey, I'm just wondering if this is supported or if there's a workaround to make this work with this library?

The goal is to have an infinite scroll list where the items update in realtime.

@vpishuk
Copy link

vpishuk commented Jan 6, 2025

https://github.com/vpishuk/react-query-firebase
This package has useInfiniteQuery hook.
Here is an example of how to use it:

const getInitialPageParam = () => {
        return by === "createdOn"
            ? direction === "desc"
                ? Infinity
                : -Infinity
            : direction === "desc"
              ? "~"
              : "";
    };

    const getNextPageParam = (lastPage) => {
        return (lastPage?.length ?? 0) > 0 ? lastPage[lastPage.length - 1].createdOn : "";
    };

    const getPrevPageParam = (firstPage) => {
        return (firstPage?.length ?? 0) > 0 ? firstPage[0].createdOn : "";
    };

   const collectionReference = useCollectionReference({ path: COLLECTION_PATH }).withConverter(converter);

    useInfiniteQuery({
        options: {
            queryKey: ['mykey'],
            initialPageParam: startAfter(getInitialPageParam()),
            getNextPageParam: (lastPage) => {
                return startAfter(getNextPageParam(lastPage));
            },
            getPreviousPageParam: (firstPage) => {
                return endBefore(getPrevPageParam(firstPage));
            },
        },
        query: collectionReference,
        queryConstraints: [orderBy(by, direction)]
    });

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

2 participants