React Native - Image Require Module using Dynamic Names

前端 未结 14 914
夕颜
夕颜 2020-11-22 11:29

I\'m currently building a test app using React Native. The Image module, thus far has been working fine.

For example, if I had an image named avatar, th

14条回答
  •  忘了有多久
    2020-11-22 12:07

    To dynamic image using require

    this.state={
           //defualt image
           newimage: require('../../../src/assets/group/kids_room3.png'),
           randomImages=[
             {
                image:require('../../../src/assets/group/kids_room1.png')
              },
             {
                image:require('../../../src/assets/group/kids_room2.png')
              }
            ,
             {
                image:require('../../../src/assets/group/kids_room3.png')
              }
            
            
            ]
    
    }
    

    when press the button-(i select image random number betwenn 0-2))

    let setImage=>(){
    //set new dynamic image 
    this.setState({newimage:this.state.randomImages[Math.floor(Math.random() * 3)];
    })
    }
    

    view

    
    

提交回复
热议问题