React Native - Image Require Module using Dynamic Names

前端 未结 14 1151
夕颜
夕颜 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: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 (
            
        )
    }
    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} />

提交回复
热议问题