简体   繁体   English

React Native - 图像组件不接受源 URI 属性的变量

[英]React Native - Image Component does not accept variable for Source URI attribute

A parent component passes down data received through a database call to the child component.父组件将通过数据库调用接收到的数据传递给子组件。

My code for the child component being rendered is as follows:我正在呈现的子组件的代码如下:

import React, { Component } from 'react'
import { Image, View, Text } from 'react-native'

class ListItem extends Component {
  render () {
    const { name, image } = this.props.item

    return <View>
      <Image
        style={{ width: 50, height: 50 }}
        source={{ uri: image }} />
      <Text>
        {name}
      </Text>
    </View>
  }
}

export default ListItem

With the code above, no image shows up, although a 50 x 50 element does take up space (seen using inspector).使用上面的代码,虽然 50 x 50 元素确实占用了空间(使用检查器看到),但没有显示图像。

The URL being tested works fine and the image shows up in the app when hard-coded as such:被测试的 URL 工作正常,图像在硬编码时显示在应用程序中:

source={{ uri: 'test URL here' }}

I've also used console.log to check that the image prop exists before the return call.我还使用console.logreturn调用之前检查image道具是否存在。 Everything indicates that the URL (ie the image prop) already exists from the database call.一切都表明 URL(即image道具)已经存在于数据库调用中。

On a side-note, I was originally using react-native's ListItem component and passing it the image prop as its avatar attribute;附带说明一下,我最初使用 react-native 的ListItem组件并将image道具作为其avatar属性传递给它; the same issue occurs where the image doesn't show up, just a grey box.图像不显示,只是一个灰色框时会出现同样的问题。 This is also fixed by setting the following:这也可以通过设置以下内容来解决:

avatar={'test URL here'}

As you tagged your question under 'react-native-ios' I suppose that you are traying your code in iOS. 当你在'react-native-ios'下标记你的问题时,我想你是在iOS中追踪你的代码。 In that case, and if your url is an 'http' one, you must check the following link: 在这种情况下,如果您的网址是“http”网址,则必须检查以下链接:

Enable App Transport Security 启用App Transport Security

Are we 100% sure image is a string?我们是否 100% 确定image是字符串? Since it works when hardcoding the url, perhaps the image prop isn't strictly a string由于它在对 url 进行硬编码时有效,因此image属性可能不是严格的字符串

As it turns out, the url being passed in from the database did have prop-type string but did not evaluate as being equivalent to a manually declared variable of the same URL. 事实证明,从数据库传入的url确实有prop-type string但没有评估为等同于同一URL的手动声明的变量。 Invoking eval(url) on the database value fixes this issue. 在数据库值上调用eval(url)可修复此问题。 If anyone has an insight as to why the following code: 如果有人对以下代码的原因有所了解:

在此输入图像描述

results in the following console output: 导致以下控制台输出:

在此输入图像描述

that would be appreciated! 不胜感激!

You can try this,你可以试试这个

class ListItem extends Component {
  render () {
    const { name, image } = this.props.item;
    const sourceUri = { uri: image };

    return (
      <View>
        <Image
          style={{ width: 50, height: 50 }}
          source={sourceUri} />
      </View>
    );
  }
}

export default ListItem;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM