1

I have a deconstructured array of values, as instructed in the docs. (react-firebase-hooks)

const [ signInWithEmailAndPassword, loading, error] = useSignInWithEmailAndPassword(auth);

All is fine, but what if I have another hook with the same return keys:

const [ signInWithEmailAndPassword, loading, error] = useSignInWithEmailAndPassword(auth);  
const [ signInWithGoogle, loading, error ] = useSignInWithGoogle(auth);

As I cannot redeclare variables, how would I solve this?

I want to eventually return a spinner, if either loading value is true.

What I tried: Assigning a new variable name inside the deconstructor.

What I was expecting: My expectations were open.

4
  • Have you tried it with [ signInWithGoogle, loading: googleLoading, error: googleError ] ? Left side of colon is the original variable name, right side is the name you want to rename it to. Commented Dec 20, 2022 at 14:12
  • 1
    Your destructirings are array destructurings; the variable names can be anything you want, because the implicit array indexes are the keys for extracting values from the source arrays. Commented Dec 20, 2022 at 14:12
  • 6
    Array destrcuturing is based on the index. You can name the variables whatever you want Commented Dec 20, 2022 at 14:12
  • @adiga ah, of course Commented Dec 20, 2022 at 14:14

2 Answers 2

1

You can reassign the names like this:

const [ signInWithEmailAndPassword, loading, error] = [true, true, false];
const [ signInWithGoogle, gLoading = loading, gErr = error ] = 
  [`Hello`, false, `error!`];
console.log( `${signInWithEmailAndPassword}, ${loading}, ${error}\n${
  signInWithGoogle}, ${gLoading}, ${gErr}`);

Sign up to request clarification or add additional context in comments.

Comments

-1

you don't have to do it like this:

const [ signInWithEmailAndPassword, loading, error] = useSignInWithEmailAndPassword(auth);

simply replace it with

 const signinObject = useSignInWithEmailAndPassword(auth);
 // do what you need with loading 
 signinObject[1]
 // do what you need with error
 sigininObject[2]

the the destruction only take it out from the array, but this way you keep it as array and refer to its index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.