question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you reading user document when logged in and skip if not

See original GitHub issue

Hi guys, This might be a simple on and I am not professional in ReactJS. Q: In my home page I am reading, the user document for the logged in user. If the user is logged in I will display some information from it. If not, that section is hidden. const [user, loading, error] = useAuthState(auth); const [value, docLoading, docError] = useDocumentOnce(firestore.doc('/users/' + user.uid), { snapshotListenOptions: { includeMetadataChanges: true }, }); It throws an error for not logged in case and if I check the user value, then throws an error: “React Hook “useDocumentOnce” is called conditionally. React Hooks must be called in the exact same order in every component render” I can create a component/separate page might be a solution. Is there a better way to do this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mauriceackelcommented, Feb 23, 2021

Hi, I think you can make use of the fact that you can pass a null value as document reference here. You might want to try the following:

const [user, loading, error] = useAuthState(auth);
const [value, docLoading, docError] = useDocumentOnce(user ? firestore.doc('/users/' + user.uid) : null, {
    snapshotListenOptions: { includeMetadataChanges: true },
});

Perhaps it would also work with:

const [user, loading, error] = useAuthState(auth);
const [value, docLoading, docError] = useDocumentOnce(user && firestore.doc('/users/' + user.uid) , {
    snapshotListenOptions: { includeMetadataChanges: true },
});
0reactions
chrisbiancacommented, Jan 31, 2022

@mauriciosoares, as @mauriceackel says, the example you give wouldn’t work when the user is not defined as user.uid would throw an error so you would have to protect against this with a ternary, or similar, anyway, so in that instance you’d just be moving the same logic elsewhere in your code.

Whilst the React Query approach is another way of doing things, I’m loathe to introduce additional complexity to the codebase and feel that the use of a null / undefined document reference is a simpler take on the same thing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check if user is already logged in and skip if it is true
My code works there is login first. However website does not ask to login in if you have already logged in. Is there...
Read more >
Skip reading files that are being written to - SAS Communities
I think the way to go is to use the FOPEN or MOPEN function. That should just return a zero if the file...
Read more >
How to Create a “Skip to Content” Link - CSS-Tricks
This post shows how to make a visible skip link, which is good. A skip link must become visible when you tab to...
Read more >
How Users Read on the Web - Nielsen Norman Group
(Update: a newer study found that users read email newsletters even more ... (users will skip over any additional ideas if they are...
Read more >
Understanding Success Criterion 2.4.1 - W3C
When this Success Criterion is not satisfied, it may be difficult for people with some disabilities to reach the main content of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found