【问题标题】:TypeError: Cannot read property 'width' of undefined when using ImageBackgroundTypeError:使用 ImageBackground 时无法读取未定义的属性“宽度”
【发布时间】:2018-12-09 18:45:54
【问题描述】:

我正在使用来自 React Native 的 ImageBackground 组件。但是,无论我选择哪种样式,总是会显示相同的错误消息。代码如下:

  render() {
return (
  <ImageBackground
    source={require('./common/Background_image.png')}
    stlye={styles.backgroundStyle}
  >
    <Text>Some text here!!</Text>
  </ImageBackground>
);

}

  backgroundStyle: {
flex: 1,
width: null,
height: null,
resizeMode: 'cover'}

Image of the error here

React-Native 版本: 反应原生 cli:2.0.1 反应原生:0.55.4

对这可能发生的事情有什么想法吗?

【问题讨论】:

  • 你为什么通过width: null, height: null,。只需将其删除并重试。
  • 已经尝试过了,但我仍然得到同样的错误

标签: reactjs react-native imagebackground


【解决方案1】:

您传递的是stlye,但正确的语法应该是style

<ImageBackground
    source={require('./common/Background_image.png')}
    style={styles.backgroundStyle}
>
  <Text>Some text here!!</Text>
</ImageBackground>

【讨论】:

  • 非常感谢!我在这个愚蠢的错误上花了大约 2 个小时!
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2019-07-05
相关资源
最近更新 更多