在使用ImageBackground时出现“TypeError: Cannot read property 'width' of undefined”错误

5

我正在使用来自 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'}

这里是错误截图

React-Native版本: react-native-cli: 2.0.1 react-native: 0.55.4

您有什么想法,为什么会出现这个错误吗?


为什么要传递 width: null, height: null?直接删除它然后再试一次。 - Helmer Barcos
已经尝试过了,但我仍然得到了相同的错误。 - marcos-cr
1个回答

7

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

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

非常感谢!我因为这个愚蠢的错误浪费了大约2个小时! - marcos-cr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接