【问题标题】:Random image after click onclass react-native单击onclass react-native后的随机图像
【发布时间】:2020-08-21 16:40:47
【问题描述】:

我的代码不起作用,我的照片没有显示。 我尝试了不同的选择,但我不明白。 也许您知道这可能会造成什么问题?

const images= [
  image2 = './assets/images/geltonas_trikampis.png',
  image3 = './assets/images/zalias_trikampis.png',
  image4 = './assets/images/juodas_trikampis.png',
  image6 = './assets/images/raudonas_trikampis.png',
  ];

  changeImage = () => {
    console.log('Working');
    const randomNumber = Math.floor(Math.random() * images.length);
    this.setState({
      currenturiImageIndex: randomNumber,
    });
  }
  
  <TouchableOpacity onPress={()=> this.changeImage()}>
      <Image
    source={this.state.currenturiImageIndex}
    style={styles.logoStyle}
  /></TouchableOpacity>

我在控制台上得到了结果,但我没有显示照片,我不知道点击后还会出现什么。

非常感谢您的帮助。

【问题讨论】:

    标签: image react-native random


    【解决方案1】:

    您正在为源属性提供一个整数值。

    请在下方更改

    source={this.state.currenturiImageIndex}
    

    source={images[this.state.currenturiImageIndex]}
    

    希望。这行得通!

    【讨论】:

      【解决方案2】:

      您绝对需要按照 Siddharth Mishra 在此处的建议进行操作,并将您的代码更改为 source={images[this.state.currentuirImageIndex]}

      如果这不是唯一的问题,您需要检查的另一件事是您为图像提供了heightwidth。您可以将它们设置为props,也可以将它们设置在style 中,但如果您不给它们指定高度和宽度,那么它们将默认为0。

      【讨论】:

      • 我没有看到图片,虽然我的代码写得很好:&lt;TouchableOpacity onPress={()=&gt; this.changeImage()}&gt; &lt;Image resizeMode="contain" style={styles.logotipas} source={images[this.state.currenturiImageIndex]} /&gt;&lt;/TouchableOpacity&gt; 我的风格是 100% 工作的。 const styles = StyleSheet.create({ container: { flex: 1 }, logotipas: { width: 100, height: 200 }我没有错误,但图像不工作。
      • 抱歉延迟响应,尝试将宽度和高度添加为道具而不是样式。
      猜你喜欢
      • 1970-01-01
      • 2023-01-03
      • 2012-12-17
      • 2017-03-27
      • 2021-04-05
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      相关资源
      最近更新 更多