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

useCollectionData loading only updating on initial render not on refetch #286

Open
JamesBrightman opened this issue Jan 3, 2023 · 0 comments

Comments

@JamesBrightman
Copy link

I am using the useCollectionData hook to grab some data from a collection. I see that the 2nd variable in the hook is to indicate loading. Logging this var I can see that loading is true on initial render (eg refreshing the page).

If you add/remove documents to a collection, I would expect loading to be true as it fetches and renders the new changes. However, it seems that it is always false after the initial render, even if you modify the underlying collection that useCollectionData is pointed at.

Here's an example;

export const App = () => {

  const [entryData, loading, error] = useCollectionData<entry>(
    query(collection(db, "entries"), orderBy("createdAt", "asc")).withConverter(entryConverter)
  );

  console.log(loading)

  const addEntryDocument = async() => {
    await addDoc(collection(db, "entries"), {createdAt: serverTimestamp(), rating: (Math.floor(Math.random()*10))} as entry)
  }

  const deleteEntryDocument = async() => {
    //Delete first element in collection
    await deleteDoc(doc(db, "entries", entryData![0].id!))
  }

  return (
    <div className="w-full flex flex-col items-center gap-2">
      {loading && <div>LOADING</div>}
      
      <button onClick={addEntryDocument}>Add record</button>
      <button onClick={deleteEntryDocument}>Delete top record</button>

      {entryData?.map((ele: entry, idx: number) => {return <div key={ele.id} className="flex flex-row items-center justify-center w-min gap-4 p-4 border border-2 border-black">
        <p>{ele.id}</p>
        <p>{ele.rating}</p>
        </div>})}
    </div>
  )
}

This maps and displays documents in the "entries" collection. It also displays a simple loading UI when the data is being fetched. This loading UI is not show when adding or removing docs, only on init load. Is this a bug, or expected behaviour? Is there a way I can get loading to update when the collection is refetched? Thanks.

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