I have images files saved locally as PNGs in my react project, and what i'm trying to do is set an image source dynamically based on the string value of a variable called "iconName"
<Image
source={iconName === "account-outline" ? {require("../../assets/images/account-outline.png")} :
{iconName === "phone-outline" ? {require("../../assets/images/phone-outline.png")} :
{iconName === "email-outline" ? {require("../../assets/images/email-outline.png")} :
{require("../../assets/images/lock-outline.png")} } } }
style={{ height: 22, width: 22, marginRight: 10 }}
/>
I'm not sure what is the correct way to format this chain of ternary operators in this situation or if there is a better alternative to using a ternary operator all together.
When I try to run the above code I get this error:
SyntaxError: C:\Users\jimmy\Documents\carebit newest\Carebit\src\utils\CustomTextInput.js: Unexpected token (41:60)
39 | >
40 | <Image
> 41 | source={iconName === "account-outline" ? {require("../../assets/images/account-outline.png")} :
| ^
42 | {iconName === "phone-outline" ? {require("../../assets/images/phone-outline.png")} :
43 | {iconName === "email-outline" ? {require("../../assets/images/email-outline.png")} :
44 | {require("../../assets/images/lock-outline.png")} } } }
From here what I tried to do is surround the whole ternary operator chain in brackets {}, and doing it that way the error moves to the "==="
Android Bundling failed 688ms SyntaxError: C:\Users\jimmy\Documents\carebit newest\Carebit\src\utils\CustomTextInput.js: Unexpected token, expected "," (41:29)
39 | >
40 | <Image
> 41 | source={{ iconName === "account-outline" ? {require("../../assets/images/account-outline.png")} :
| ^
42 | {iconName === "phone-outline" ? {require("../../assets/images/phone-outline.png")} :
43 | {iconName === "email-outline" ? {require("../../assets/images/email-outline.png")} :
44 | {require("../../assets/images/lock-outline.png")} } } }}
Is there a particular way I should be formatting this, am i placing the brackets wrong? Is it even possible to do this with an image source?
