FirebaseExtended / FirebaseExtended/reactfire

Allow `useFirestoreDoc` `DocumentReference` arguments to be nullable

未关闭
#463 13 条评论 18 个 reaction 已指派 0 人 在 GitHub 查看
v5
主要语言
TypeScript
星标
3.6k
派生
403
平均合并
14 小时 53 分钟
30 天内合并 PR
5

描述

`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.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。