简体   繁体   中英

React-Native Dynamic Image Source?

I'm trying to gather image sources from a JSON file but whenever I use a JSON objects source I get a module error. Is there any workaround for this?

JSON FILE:

 {
   "Image": {"source": "./SourceFolder/ImageFile.png"}
 }

JS FILE

 const file = require('./jason.json');
 var jason = JSON.stringify(file.Image);
 jason = JSON.parse(jason);

 <Image source={require(jason.source)} />

Dynamic requires are not supported unfortunalty. You need to pre-require all of your images, then you're able to dynamically call them from a reference.

const images = {
  image1: require('some_file.png'),
}

<Image source={images["image1"]} />

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