invertase / invertase/tanstack-query-firebase

[Question] Dependant queries and paths

Open
#31 2 comments 0 reactions 0 assignees View on GitHub
react-query-firebase
Dominant language
TypeScript
Stars
462
Forks
69
PR merge metrics
No merged PRs in 30d

Description

I was wondering how you are dealing with a segment of the path being undefined on the first render when one refreshes the page or opens a copy pasted deep link in a new tab

I just did it like this, but im sure there must be a better pattern?

```
const useGetItem = ({ id = '' }) => {
const { user } = useAuth();

// Necesary evil for now because the query value of nextjs router is initially empty and rules of hooks discourage conditional calling.

const path = user?.uid ? `users/${user?.uid}/items/${id}` : 'users/1';

const itemDocReference = doc(firestore, path).withConverter(ItemConverter);

return useFirestoreDocumentData(
['item', id],
itemDocReference,
{ idField: 'id' },
{ enabled: !!user?.uid }
);
};
```

```
const useGetUser = () => {
const { user } = useAuth();
const userDocReference = doc(firestore, `users/${user?.uid}`).withConverter(
UserConverter
);

return useFirestoreDocumentData(
['users'],
userDocReference,
{
idField: 'uid'
},
{
enabled: !!user?.uid
}
);
};
```

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.