React Native - Image Require Module using Dynamic Names

前端 未结 14 1068
夕颜
夕颜 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:11

    Important Part here: We cannot concat the image name inside the require like [require('item'+vairable+'.png')]

    Step 1: We create a ImageCollection.js file with the following collection of image properties

    ImageCollection.js
    ================================
    export default images={
        "1": require("./item1.png"),
        "2": require("./item2.png"),
        "3": require("./item3.png"),
        "4": require("./item4.png"),
        "5": require("./item5.png")
    }
    

    Step 2: Import image in your app and manipulate as necessary

    class ListRepoApp extends Component {
    
        renderItem = ({item }) => (
            
                Item number :{item}
                
            
        );
    
        render () {
            const data = ["1","2","3","4","5"]
            return (
                
            )
        }
    }
    
    export default ListRepoApp;
    

    If you want a detailed explanation you could follow the link below Visit https://www.thelearninguy.com/react-native-require-image-using-dynamic-names

    Courtesy : https://www.thelearninguy.com

提交回复
热议问题