r/reactjs • u/povedaaqui • 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
2
u/galactic_dust Jul 14 '22
For people who will visit this later reply to the correct solution.
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.
1
u/Cyberhunter80s Jul 14 '22
Console log the result first before all this logic. Is it true?
1
u/_codemonger Jul 14 '22
Are address and seller strings or objects?
1
u/_codemonger Jul 14 '22
Why are you wrapping them in template literals then?
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():
0
u/[deleted] Jul 14 '22
[deleted] — view deleted comment
0
u/[deleted] Jul 14 '22
[deleted] — view deleted comment