0

Question #1:

How to create a uri with a link and a variable? My example is from an API.

<Image source={{uri: 'http://www.mypage.com/'+`${val.images.filename}`}}  style={styles.imgProd} />

Question #2:

I would also like to know how to build the uri with a function like something like this.

makeImgUrl(value) {
    if (value === '' || valueUrl === 'null') {
        return ("http://www.mypage.com/defaultimg.jpg")
    } else {
        return ("http://www.mypage.com/"+${value})
    }
}

<Image source={{uri: +makeImageUrl(val.images.filename)}}  style={styles.imgProd} />

2 Answers 2

1

You are using the string interpolation wrongly.

let name = 'John';

console.log('Hi! I am ' + name); //ES5

console.log(`Hi! I am ${name}`)

As shown above, either we go for ES5 way, which we don't use backtick at all, or we opt for ES6 way, remove the single-quote/double-quote and replace with backtick.

So apply whatever I said onto your scenario, it would be as below

<Image source={{uri: 
`http://www.mypage.com/${val.images.filename}`}}  style={styles.imgProd} />

or

<Image source={{uri: 
'http://www.mypage.com/' + val.images.filename}}  style={styles.imgProd} />
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply add variable with url concating with + like below :

<Image source={{uri: 'http://www.mypage.com/'+val.images.filename}}  style={styles.imgProd}/>

OR call function like :

function makeImgUrl(value) {
    if (value === '' || valueUrl === 'null') {
        return "http://www.mypage.com/defaultimg.jpg"
    } else {
        return "http://www.mypage.com/"+value
    }
}

<Image source={{ uri: makeImageUrl(val.images.filename) }}  style={styles.imgProd} />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.