简体   繁体   English

图像uri无法按预期运行-React Native

[英]Image uri not working as expected - React Native

I am learning React Native. 我正在学习React Native。 Following the guide, Getting Started of Facebook ( https://facebook.github.io/react-native/docs/props ). 遵循指南,Facebook入门( https://facebook.github.io/react-native/docs/props )。 When I tried to make a new component containing an image sometimes works and sometimes not. 当我尝试制作一个包含图像的新组件时,有时可以工作,有时则不能。 I have read online that this is due to how they are load, React Native doesn't support dynamic loading of images. 我已经在线阅读了这是由于它们如何加载,React Native不支持图像的动态加载。

class CuteImage extends Component {
  render() {

    return (
      <View>
        <Image source={{uri: 'https://cdn1.medicalnewstoday.com/content/images/articles/322/322868/golden-retriever-puppy.jpg'}} />
      </View>
    );
  }
}

export default class ExampleComponent extends Component {
  render() {
    return (
      <View style={{alignItems: 'center', justifyContent: 'center', flex: "1", flexDirection: "column"}}>
        <CuteImage />
      </View>
    );
  }
}

I expected this to work fine because in the Image Component tutorial they did something similar and it had worked ( https://facebook.github.io/react-native/docs/image ) but it throws me the following error: 我希望它能正常工作,因为在“图像组件”教程中,他们做了类似的事情,并且已经起作用了( https://facebook.github.io/react-native/docs/image ),但是它引发了以下错误:

Failed to construct 'Image': Please use the 'new' operator

Why is this happening? 为什么会这样呢? How dynamic and static loading relates to this? 动态和静态加载与此有何关系?

I think you forgot to add dimensions for your image. 我认为您忘记为图像添加尺寸。 The docs say here that you have to provide them when using network and data images. 文档在这里说,使用网络和数据映像时必须提供它们。 I attached a screenshot of the result, it works now. 我附上了结果的屏幕截图,现在可以使用了。

<View>
    <Image style={{width: 380, height: 340} } source={{uri: 'https://cdn1.medicalnewstoday.com/content/images/articles/322/322868/golden-retriever-puppy.jpg'}} />
</View>

Cheers. 干杯。

应用截图

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

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