0

I have the following conditional

if (
    !user.userId
    && match.path === '/login'
) {
    component = <Login/>
} else if (
    user.userId
    && !user.OTPVerified
    && !user.loginWithPassword
    && match.path === '/verify'
) {
    component = <VerifyOTP/>
} else if (
    (user.userId && user.OTPVerified) || (user.userId && user.loginWithPassword)
    && !user.profileCompleted
    && match.path === '/complete-profile'
) {
    console.log('userid', user.userId)
    console.log('otpverified', user.OTPVerified)
    console.log('loginWithPassword', user.loginWithPassword)
    console.log('profileCompleted', user.profileCompleted)
    console.log('path', match.path)
    component = <CompleteProfile/>
} else if (
    user.userId
    && !user.OTPVerified
    && user.loginWithPassword
    && user.profileCompleted
    && match.path === '/login-password'
) {
    component = <LoginWithPassword/>
} else {
    component = <Login/>
}

console returns

userid 29
otpverified true
loginWithPassword false
profileCompleted true
path /login

I dont get why am i still seeing CompleteProfile component

4
  • 1
    as per your output, your if hits this else if ( (user.userId && user.OTPVerified) , what's your expected output? Commented Dec 16, 2020 at 20:50
  • @Nonik thats whats confusing, it shouldn't because yes (user.userId && user.OTPVerified) is true but && !user.profileCompleted is also true so it should just go to else Commented Dec 16, 2020 at 20:54
  • Your && and || operators have the same precedence and therefore are checked left to right. Since (user.userId && user.OTPVerified) evaluates to true and therefore the condition of your if is true || false && false && false. That is logically equivalent to true || (false && (false && false)) which evaluates to true Commented Dec 16, 2020 at 20:55
  • @ChadS.okay so how do i set the proper precedence Commented Dec 16, 2020 at 20:57

1 Answer 1

1

Add some parenthesis around those two expressions with the || between them if they should be evaluated together.

((user.userId && user.OTPVerified) || (user.userId && user.loginWithPassword))

This change makes your Login component show based on the values.

You can remove the inner ones too and the && will be evaluated first.

(user.userId && user.OTPVerified || user.userId && user.loginWithPassword)

else if (
    (user.userId && user.OTPVerified || user.userId && user.loginWithPassword)
    && !user.profileCompleted
    && match.path === '/complete-profile'
)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#Operator_precedence

console.log((true && true) || (true && false) && false && false) //true
console.log((true && true || true && false) && false && false) // false
Sign up to request clarification or add additional context in comments.

2 Comments

No you don't, you have them around each one, not around both.
oh, wonderful that solves the problem, thank you so much!!

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.