So I have a footer component that reads links from a JSON file. I have a particular header that I only want to have present if the corresponding JSON property is not blank (i.e., if there’s no link provided, it won’t show up.) My current setup looks a little like this:
{#if (socialMediaLinks["contactUs"] !== "")}
<a href={socialMediaLinks["contactUs"]}>
<h2>Contact Us</h2>
</a>
{/if}
Issue being, this code results in the h2 always being visible, even with no link provided. If i try and alter the if to
{#if (socialMediaLinks["contactUs"] === "")}
the link doesn’t show even if the property isn’t blank.
This behavior feels backwards. Am I missing something?
2
Answers
The problem lies in your IF, I’m guessing. The value of
socialMediaLinks["contactUs"]
might be eithernull
orundefined
and you are requesting exact comparison (triple operator).This should work: