I have a custom auth hook that is mean to provide the user and loading state, as well as some user based actions (such as signing out). Here is my code:
type UserActions = {
signOut(): Promise<void>;
};
const useUser = (): [
firebase.User | undefined | null,
UserActions,
boolean,
firebase.auth.Error | undefined
] => {
const auth = firebase.auth();
const [firebaseUser, loading, error] = useAuthState(auth);
return [firebaseUser, { signOut: firebase.auth().signOut }, loading, error];
};
export default useUser;
When I use the signOut() function in another file after invoking the hook, I get an error Unhandled Rejection (TypeError): Cannot read property 'then' of undefined.
However, when I simply change my hook to be like so:
type UserActions = {
signOut(): Promise<void>;
};
const useUser = (): [
firebase.User | undefined | null,
UserActions,
boolean,
firebase.auth.Error | undefined
] => {
const auth = firebase.auth();
const [firebaseUser, loading, error] = useAuthState(auth);
const signOut = () => {
return firebase.auth().signOut();
};
return [firebaseUser, { signOut }, loading, error];
};
export default useUser;
That works perfectly. What is the difference between these two cases? Not sure if I'm missing something about how arrow functions work, or maybe something with typing?
Thanks!