reactfire icon indicating copy to clipboard operation
reactfire copied to clipboard

Usage of useUser with useFirestoreDocData not very compatible within custom hooks

Open dennemark opened this issue 4 years ago • 4 comments

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

dennemark avatar Aug 20 '21 15:08 dennemark

Thanks for your thoughtful feedback, @dennemark. I wonder if we should consider a version of useUser that always returns a user (and throws an error if one doesn't exist).

For now, I agree that the best workaround is probably to pass the user in as a prop (unless someone else has a better solution).

Or, if you're using TypeScript, maybe cast it?

const {data: user} = useUser(); 
const ref = doc(firestore, "users", (user as User).uid);

jhuleatt avatar Aug 25 '21 21:08 jhuleatt

Any updates on this because Im having the same issue?

Gnadhi avatar Apr 08 '22 13:04 Gnadhi

Since the discussion is already a few months old, I don´t remember if I forgot to reply. I went back to firebase v8 and reactfire v3. Does the proposed solution of @jhuleatt work for you?

It seems like it is ok to wrap a hook within

try{ 
 const {data: userData} = useFirestoreDocData(ref);
} catch(error){
}

maybe this is a way of dealing with errors, if there are any.

dennemark avatar Apr 08 '22 14:04 dennemark

you can't wrap a hook in a try catch. That violates the rules of hooks.

steveoh avatar Sep 11 '23 18:09 steveoh