useDocumentData returns undefined on first render until forced browser refresh returns data
See original GitHub issueimport { doc } from 'firebase/firestore';
import { createContext, useEffect, useState } from 'react';
import { auth, db } from '../firebase/firebase-config';
import {
useDocumentData,
useDocumentDataOnce,
} from 'react-firebase-hooks/firestore';
import { useAuthState } from 'react-firebase-hooks/auth';
const userAuthContext = createContext({});
const UserAuthContextProvider = ({ children }: any) => {
const [authUser, loadingAuthUser, errorAuthUser] = useAuthState(auth);
const [authUserDocRef, setAuthUserDocRef]: any = useState();
const [userData, loading, error, snapshot, reload] =
useDocumentDataOnce(authUserDocRef);
const [businessDocRef, setBusinessDocRef]: any = useState();
const [
businessData,
loadingBusinessData,
errorBusinessData,
snapshotBusinessData,
] = useDocumentData(businessDocRef);
useEffect(() => {
if (authUser) {
setAuthUserDocRef(doc(db, 'users', authUser.uid));
}
if (userData) {
setBusinessDocRef(userData.managedBusiness);
}
}, [authUser, userData]);
return (
<userAuthContext.Provider
value={{ authUser, businessData, loadingBusinessData }}
>
{children}
</userAuthContext.Provider>
);
};
export { userAuthContext, UserAuthContextProvider };
businessData returns undefined. I have to manually force refresh the browser for businessData to return the correct value from Firestore.
Issue Analytics
- State:
- Created a year ago
- Comments:19 (2 by maintainers)
Top Results From Across the Web
Data becomes undefined in usequery of apollo client only ...
I am already using the conditional logic in the render, and it works on the first render, only on refresh the error occurs....
Read more >useQuery returns undefined on first render when data ... - GitHub
I use a piece of code high up in my component tree as such: const { data: } = useQuery(GET_CART); if (!data.cart) return...
Read more >Getting data | Firestore - Google Cloud
When you set a listener, Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document...
Read more >first time, undefined error, but after reload, it success-Reactjs
Why I am getting this error message when I run react js project first time in my system? How to do force reload...
Read more >SAP S/4HANA 2021
SAP S/4HANA Enterprise Management is designed for enterprises across ... ous dimension combinations to verify the data quality before.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi everybody, apologies for the delay in investigating this properly. I’ve just given this a try and it appears that some of the changes I’ve made in the newly released v5.1.0 may have inadvertently resolved this issue.
Could I ask you to try v5.1.0 and check whether this has been fixed?
I am also encountering this issue using React v18. I’ve switched from
useDocumentDataOnce
touseDocumentData
as a temporary workaround.