Reactjs, How to compare props.location.pathname to a string?

11,892

Solution 1

type of both the operands should be same while using == for comparision.Make sure both are of string type or change if to

if (props.location.pathname != '/confirm') {
// redirect to /confirm to force the user to confirm their email

}

Solution 2

To compare two strings in React.js, you can simply use triple-equals (or ===) as below:

if (stringTemp === 'desired string'){
      console.log('Equal');
    }
Share:
11,892
AnnaSm
Author by

AnnaSm

Updated on June 04, 2022

Comments

  • AnnaSm
    AnnaSm about 2 years

    In my router, I need to do the following:

    if (props.location.pathname !== '/confirm') {
        // redirect to /confirm to force the user to confirm their email
    }
    

    The if statement is not acting as expected.

    If I output:

    console.log(props.location.pathname)
    

    I get in the console.

    /confirm
    

    However, props.location.pathname with the value of '/confirm' is not being seen as the same as /confirm

    What am I doing wrong?