【问题标题】:React native ListItem image url passing variable反应原生 ListItem 图像 url 传递变量
【发布时间】:2018-08-20 06:30:11
【问题描述】:

我想将一些变量传递给我的头像网址,但这似乎不起作用:

renderItem = ({ item }) => (
 <ListItem
      title= {item.name}
      subtitle={
        <View style={styles.subtitleView}>
          <Text style={styles.ratingText}>Price: {item.price}</Text>
          <Text style={styles.ratingText}>Stock: {item.stock}</Text>
        </View>
      }
     avatar={{ uri: 'https://test.com/${item.ver}/${item.cid}.jpg',}}
    />
)

这是为了渲染我的ListItem,我需要这些变量来为不同的项目获取不同的头像图片,但这对我不起作用。还有其他方法吗?

【问题讨论】:

  • 你需要使用反引号而不是单引号:avatar={{ uri: `test.com/${item.ver}/${item.cid}.jpg`,}}
  • errm 这是什么意思?
  • 单引号是这样的:'反引号是这样的:`

标签: javascript reactjs react-native reactive-programming


【解决方案1】:

这是您的要求的预览。 https://codesandbox.io/s/3v0qn6j0zp 。 ES6 提出了一种新类型的字符串文字,使用反引号作为分隔符。这些文字确实允许嵌入基本的字符串插值表达式,然后自动解析和评估..

    let fb = "facebook";

    <Image
        source={{
          uri: `https://${fb}.github.io/react-native/docs/assets/favicon.png`
        }}
      />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    相关资源
    最近更新 更多