简体   繁体   中英

how can I pass multiple image with require in react-native

I want to upload multipale image in react native with map. and when I want to pass urls of local images to map,and when I call it I got this error

Invalid call at line 62: require(url)

here is my code

export default class App extends React.Component {


   state = {
    a: ["./assest/image1.jpg" , "./assest/image2.jpg","./assest/image3.jpg"],
  }

render() {
    return (
       <ScrollView style={styles.body}>
       {
          this.state.a.map((url,key) => 
             <View style ={ styles.body}>
                 <Image 
                    style={styles.image} 
                    source={require(url)}
                  />
             </View>
          )
       }
       </ScrollView>
    )
} 
}

I'm beginner in react native,how can I fix this issue. Is that anyway to call multiple images with require or no I should test another method or functions.

You can't use require with a param. Insted use it in your array:

state = {
    a: [
        require("./assest/image1.jpg"),
        require("./assest/image2.jpg"),
        require("./assest/image3.jpg")
    ]
}

// In your map on image source
source={url}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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