ReemTek
ReemTek

Reputation: 21

Return an image element only if it exists, or else return an icon -React Native

Good day!

I'm quite new to React/ReactNative (and programming in general), and I'm currently working on a project that requires me to return an image element if a certain condition is met, or else return an icon. So for example, in a pre-existing component: if a post has a photo attached, return <Image uri {photo} >, and if the post has no photo, then return a icon. Is this doable, if yes how do I go about doing that?

Thank you in advance!!

Upvotes: 1

Views: 1027

Answers (1)

kanjurer
kanjurer

Reputation: 51

Use conditional rendering:

{photo ? <Image url={photo}> : <Icon />}

Useful Link: https://reactjs.org/docs/conditional-rendering.html

Upvotes: 2

Related Questions