FirebaseExtended / FirebaseExtended/reactfire
Usage of useUser with useFirestoreDocData not very compatible within custom hooks
- 主要语言
- TypeScript
- 星标
- 3.6k
- 派生
- 403
- 平均合并
- 14 小时 53 分钟
- 30 天内合并 PR
- 5
描述
### Version info
**React:** 18 experimental
**Firebase:** 9
**ReactFire:** 4
**Other (e.g. Node, browser, operating system) (if applicable):**
### Test case
### Steps to reproduce
```
const useMyHook= () => {
const firestore = useFirestore();
const {data: user} = useUser();
const ref = user && doc(firestore, "users", user.uid);
const {data: userData} = useFirestoreDocData(ref);
return userData;
}
```
### Behavior
Since useUser is returning the type `User | null` , we need to check, if the user is available. But in the case when the user is null, useFirestoreDocData cannot be called, since the ref would not exist. In react we are not allowed to write:
` const {data: userData} = ref && useFirestoreDocData(ref) `
One way to get around this issue is by passing in the user as a prop, but it does not seem to be convenient, if one wants to use this hook within another hook. We would just push the issue upwards until we reach a component that is again wrapped by a component that knows if the user exists:
- component that checks if user exists (like deprecated AuthCheck)
- component that is rendered when user exists
- hook that is called within that component
Another alternative for typescript is writing `user!` with exclamation mark, but this does not feel proper.
I hope the case is clear and you might know a better way to deal with it.
I think with react-fire v3 (fb v8) I wasn't experiencing this issue. Since it was probably solved with this issue: https://github.com/FirebaseExtended/reactfire/issues/249
贡献指南
调研方向
Start by tracing the useUser, useFirestore, and useFirestoreDocData entry points and reproduce the null-user path with the custom hook shown in the issue. Compare the reported ReactFire v4 behavior with the ReactFire v3 behavior referenced in issue 249; the issue does not name a file, test, or concrete completion criterion.
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- firebase, react, typescript
- 领域
- authentication, database, frontend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100