React Native - Image Require Module using Dynamic Names

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

    You should use an object for that.

    For example, let's say that I've made an AJAX request to an API and it returns an image link that I'll save to state as imageLink:

    source={{uri: this.state.imageLink}}

    0 讨论(0)
  • 2020-11-22 12:05

    Say if you have an application which has similar functionality as that of mine. Where your app is mostly offline and you want to render the Images one after the other. Then below is the approach that worked for me in React Native version 0.60.

    1. First create a folder named Resources/Images and place all your images there.
    2. Now create a file named Index.js (at Resources/Images) which is responsible for Indexing all the images in the Reources/Images folder.

    const Images = { 'image1': require('./1.png'), 'image2': require('./2.png'), 'image3': require('./3.png') }

    1. Now create a Component named ImageView at your choice of folder. One can create functional, class or constant component. I have used Const component. This file is responsible for returning the Image depending on the Index.
    import React from 'react';
    import { Image, Dimensions } from 'react-native';
    import Images from './Index';
    const ImageView = ({ index }) => {
        return (
            <Image
                source={Images['image' + index]}
            />
        )
    }
    export default ImageView;
    
    1. Now from the component wherever you want to render the Static Images dynamically, just use the ImageView component and pass the index.

      < ImageView index={this.qno + 1} />

    0 讨论(0)
  • 2020-11-22 12:07

    you can use

    <Image source={{uri: 'imagename'}} style={{width: 40, height: 40}} />
    

    to show image.

    from:

    https://facebook.github.io/react-native/docs/images.html#images-from-hybrid-app-s-resources

    0 讨论(0)
  • 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

    <Image
            style={{  width: 30, height: 30 ,zIndex: 500 }}
            
            source={this.state.newimage}
          />
    
    0 讨论(0)
  • 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 }) => (
            <View style={styles.item}>
                <Text>Item number :{item}</Text>
                <Image source={Images[item]}/>
            </View>
        );
    
        render () {
            const data = ["1","2","3","4","5"]
            return (
                <FlatList data={data} renderItem={this.renderItem}/>
            )
        }
    }
    
    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

    0 讨论(0)
  • 2020-11-22 12:15

    If you're looking for a way to create a list by looping through a JSON array of your images and descriptions for example, this will work for you.

    1. Create a file (to hold our JSON database) e.g ProfilesDB.js:

    const Profiles=[
    	  {
    	  "id" : "1",
    	  "name" : "Peter Parker",
    	  "src" : require('../images/user1.png'),
    	  "age":"70",
    	},
    	{
    	"id" : "2",
    	"name" : "Barack Obama",
    	"src" : require('../images/user2.png'),
    	"age":"19",
    	},
    	{
    	"id" : "3",
    	"name" : "Hilary Clinton",
    	"src" : require('../images/user3.png'),
    	"age":"50",
    	}
    ]
    export default Profiles;

    1. Then import the data in our component and loop through the list using a FlatList:

    import Profiles from './ProfilesDB.js';
    
    <FlatList
      data={Profiles}
      keyExtractor={(item, index) => item.id}
      renderItem={({item}) => (
        <View>
          <Image source={item.src} />
          <Text>{item.name}</Text>
        </View>
      )}
    />

    Good luck!

    0 讨论(0)
提交回复
热议问题