Allow `useFirestoreDoc` `DocumentReference` arguments to be nullable
See original GitHub issuereact-firebase-hooks
allows the document reference to useDocument
to be nullable and just returns undefined if it is. This was useful for paths relying on nullable info, like auth.currentUser.uid
:
const [snapshot, loading, error] = useDocument(auth.currentUser && firestore.doc(`users/${auth.currentUser.uid}`));
where if the user were not signed in there wouldn’t be an error thrown since useDocument
would just return undefined
. It would be nice if a similar feature were added to reactfire, as
const { status, data: firebaseDoc } = useFirestoreDoc(auth.currentUser && doc(firestore, 'users', auth.currentUser.uid));
won’t work as the type of ref is DocumentReference
, not DocumentReference | null
,
const { status, data: firebaseDoc } = useFirestoreDoc(doc(firestore, 'users', auth.currentUser?.uid));
will throw an error when the user is not signed in (as the path will become invalid), and
if (auth.currentUser) {
const { status, data: firebaseDoc } = useFirestoreDoc(doc(firestore, 'users', auth.currentUser.uid));
}
violates the rules of hooks. Currently relying on a rather abhorrent workaround to resolve this and it would be ideal if reactfire could support this behavior natively.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:12
- Comments:8 (2 by maintainers)
Top Results From Across the Web
The non-nullable local variable 'docRef' must be assigned ...
This line declared a variable that has to be a DocumentReference , and can't be null : DocumentReference docRef;.
Read more >reactfire - npm
We need to do this because useFirestoreDoc throws a Promise while it is waiting for a response from Firestore. Suspense will catch the...
Read more >DocumentReference | Node.js (client) API reference
Reference for DocumentReference. ... Passing in null as the converter parameter removes the current converter. Parameters. converter: null.
Read more >FirebaseExtended Reactfire Statistics & Issues - Codesti
Issue Title State Comments Created Date Updated Date
StackBlitz live demo not working (locally too) open 0 2022‑12‑19 2022‑12‑23
403 on storage request open 2...
Read more >Dart Null Safety: The Ultimate Guide to Non-Nullable Types
Before addressing Null Safety, let's talk about the Dart type system. ... square(null); // The argument type 'Null' can't be assigned to the...
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 @jhuleatt , I have read your comments in #178. I do however believe that there is still a necessity for this.
Specifically, in our codebase, we have a number of documents referencing each other i.e.
Rendering requires getting a number of documents which is not easily broken down into subcomponents. In our case it would require nesting a number of levels deep.
Having a nullable or undefined argument will greatly simplify things. We do have undefined and null in javascript to distinguish to enable the API to distinguish between not yet resolved and not available.
useFirestoreDoc(null)
can still return a doc but withdoc.data() == null
anddoc.exists() == false
Hi there - thanks for the very helpful lib @jhuleatt.
Just jumping in as there is actually a use-case where breaking into small components might not be convenient. This happens whenever you want to feed a component with remote default values if they exist, I think it is actually a quite common use-case.
Just to illustrate simply this use-case, say we have a public contact form page.
In this very case, breaking into small components might not be very practical as we need to create dedicated wrapper component only to fetch the data and populate the default values.
Happy to get your views, thanks.
(Disclaimer: I might not yet very familiar with ReactFire, please forgive me if I miss understood its usage philosophy).