FirebaseExtended / FirebaseExtended/reactfire

Allow `useFirestoreDoc` `DocumentReference` arguments to be nullable

Open
#463 13 comments 18 reactions 0 assignees View on GitHub
v5
Dominant language
TypeScript
Stars
3.6k
Forks
403
Avg merge
14h 53m
Merged PRs (30d)
5

Description

`react-firebase-hooks` allows [the document reference to `useDocument` to be nullable](https://github.com/CSFrequency/react-firebase-hooks/tree/master/firestore#usedocument) and just returns undefined if it is. This was useful for paths relying on nullable info, like `auth.currentUser.uid`:
```js
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
```js
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`,
```js
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
```js
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.