【问题标题】:specifying image src as dynamic state variable in React Native在 React Native 中将图像 src 指定为动态状态变量
【发布时间】:2018-07-26 12:23:11
【问题描述】:

我想在本机反应中加载图像。我浏览过很多帖子,但几乎每篇帖子都谈到将图片的静态/本地 url 指定为来源。但是,我想将其指定为

src= {this.state.url}

this.state.url 在代码中被分配了一个动态 url(来自 google 图片)。

有可能吗?

【问题讨论】:

  • 你绝对可以在 React Native 中使用外部图像,例如<Image source={{ uri: 'https://example.com/image.png' }} />,但要求图像不能是动态的,因为 React Native 必须知道在最终构建中要捆绑哪些图像。

标签: image react-native url dynamic native


【解决方案1】:

试试 Tholle 答案

图片来源={{ uri: 'https://example.com/image.png' }}

但是在你的 render() 中让它改变:

render(){

 var image = ( <Image source={{this.state.url}} /> );

 return(

  <View>

   {image}

  </View>

 )

}

因此,每当您的组件状态发生变化时,都会使用我认为更新后的 url 调用 render

【讨论】:

  • 有效!!但是我希望它不是从 this.state.url 中获取的,而是从具有诸如 this.state.dict[2].url 之类的随机元素的字典中获取。有可能吗?
  • 当然是 :) 你现在只需要一个有效的 URL 就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多