FirebaseExtended / FirebaseExtended/reactfire

Allow `useFirestoreDoc` `DocumentReference` arguments to be nullable

Ouverte
#463 13 commentaires 18 réactions 0 personnes assignées Voir sur GitHub
v5
Langage dominant
TypeScript
Étoiles
3.6k
Forks
403
Merge moyen
14 h 53 min
PR mergées (30 j)
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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.