FirebaseExtended / FirebaseExtended/reactfire
Allow `useFirestoreDoc` `DocumentReference` arguments to be nullable
- Ngôn ngữ chính
- TypeScript
- Star
- 3.6k
- Fork
- 403
- Merge trung bình
- 14 giờ 53 phút
- Pull request đã merge (30 ngày)
- 5
Mô tả
`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.
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.