React Native - Image Require Module using Dynamic Names

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

    First, create a file with image required - React native images must be loaded this way.

    assets/index.js

    export const friendsandfoe = require('./friends-and-foe.png'); 
    export const lifeanddeath = require('./life-and-death.png'); 
    export const homeandgarden = require('./home-and-garden.png');
    

    Now import all your assets

    App.js

    import * as All  from '../../assets';
    

    You can now use your image as an interpolated value where imageValue (coming in from backend) is the same as named local file ie: 'homeandgarden':

    
    

提交回复
热议问题