r/reactjs • • Jul 13 '22

Needs Help Conditional rendering is not working as expected

Hello,

I'm trying to compare two strings but I get always false, in this case, it means strings are different, but they aren't.

address is the same as seller, but I always get the opposite.

Both address and seller are coming from another page using location. Why may I be getting always false?

main.js

const [compare, setCompare] = useState(null);

let location = useLocation();
const {address} = location.state.address;
const {seller} = location.state.seller;

useEffect(() => {
        const compare = () => {
            if(`${address}` === `${seller}`){
                    setCompare(true);
                } else {
                    setCompare(false);
                }
            }
            seller!==null && address!=null && address!=undefined && seller!=undefined && compare()
    }, [seller])


console.log(compare) // false
1 Upvotes

18 comments sorted by

2

u/galactic_dust Jul 14 '22

For people who will visit this later reply to the correct solution.

1

u/povedaaqui Jul 14 '22 edited Jul 14 '22

I got it! I've checked the two strings carefully, and I find out one of the sources is sending one letter uppercase instead of lowercase (there are too many characters). Thank all of you for your help!

Here's the code:

seEffect(() => {
    const compare = () => {
        if(address && seller && address.toLowerCase() === seller.toLowerCase()){
                setCompare(true);
            } else {
                setCompare(false);
            }
        }
        compare()
}, [seller, address])

11

u/Cautious_Variation_5 Jul 14 '22

This code seems unnecessarily complex. What about just this without effect. I don't see a need to use an effect and state here since you're receiving the prop from a router state.

if(address && seller && address === seller) {
 // do something
}

1

u/Cyberhunter80s Jul 14 '22

Console log the result first before all this logic. Is it true?

1

u/povedaaqui Jul 14 '22

No, it's false, but the intermediate variables seems to be good.

1

u/_codemonger Jul 14 '22

Are address and seller strings or objects?

1

u/povedaaqui Jul 14 '22

strings indeed.

1

u/_codemonger Jul 14 '22

Why are you wrapping them in template literals then?

1

u/povedaaqui Jul 14 '22

just in case, but they're strings, without template literals, still not working, it's kinda the same in this case.

0

u/there_was_a_problem Jul 13 '22 edited Jul 13 '22

Looks like you need to update your dependency array with all the values you’re using I your useEffect():


const [compare, setCompare] = useState(null);

let location = useLocation();
const {address} = location.state.address;
const {seller} = location.state.seller;

useEffect(() => {
        const compare = () => {
            if(`${address}` === `${seller}`){
                    setCompare(true);
                } else {
                    setCompare(false);
                }
            }

            // this will always run compare() and it’s expression isn’t used
            seller!==null && address!=null && address!=undefined && seller!=undefined && compare()
    }, [seller, address])


console.log(compare) // false

1

u/povedaaqui Jul 14 '22

I've tried, but still the same. I'm getting false even though both address and seller are the same.

2

u/there_was_a_problem Jul 14 '22

Try simplifying you useEffect and then debug:

useEffect(() => {
  if(address && seller && address === seller){
    setCompare(true);
  } else {
    setCompare(false);
   }
}, [seller, address])

If you still getting errors those two value may not actually be the same, at least according to JS

1

u/povedaaqui Jul 14 '22

Thank you, I'll try in a bit.

0

u/[deleted] Jul 14 '22

[deleted] — view deleted comment

1

u/galactic_dust Jul 14 '22

@povedaaqui this is most probably the case. Remove curly braces to access value at location.state.address

1

u/povedaaqui Jul 14 '22

I'll check but if I print the intermediate variables, I get both variables properly by console.

These are the params I'm passing with router:

const params = {'name': {name}, 'description': {description}, 'image': {url}, 'tokenId': {tokenId}, 'contractAddress': {contractAddress}, 'address': {address},
'listingId': {listingId}, 'seller': {seller}, 'listedPrice': {listedPrice}}

1

u/galactic_dust Jul 14 '22

Put you code in sandbox and share it here. Lets have a look at it.

1

u/povedaaqui Jul 14 '22

if(address && seller && address === seller)

I'm getting this, so, I'm doing the right thing.

{address: '0x...45c57be'}

0

u/[deleted] Jul 14 '22

[deleted] — view deleted comment

1

u/povedaaqui Jul 14 '22

I'll try. You have found some typos as well. Thanks