简体   繁体   English

尝试从json文件加载位置时未显示React Native图像

[英]React Native image not showing when try to load location from json file

This is a json file that stores a title and a image location for every elements. 这是一个json文件,其中存储每个元素的标题和图像位置。

 [ {"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"}, {"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"}, {"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"}, {"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"}, {"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"}, {"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"}, {"key": "g", "title": "Others", "image": "require('./img/m.jpg')"}, ] 

And Now I want to show all image and title using a Flatlist. 现在,我想使用平面列表显示所有图像和标题。 Title shows perfectly but image not shows. 标题显示完美,但图像未显示。

 <FlatList horizontal= {true} data={newsCategory} renderItem={({item}) => ( <TouchableOpacity style={styles.option} > <Image style={styles.imgButton} source={item.image} /> <Text style={styles.buttonText}>{item.title}</Text> </TouchableOpacity> ) } /> 

I check that image location comes perfectly from json file but not shows the image. 我检查图像位置是否完全来自json文件,但未显示图像。 Don't understand why this occurs. 不明白为什么会这样。

remove string from require. 从require中删除字符串。

change this 改变这个

[
    {"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"},
    {"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"},
    {"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"},
    {"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"},
    {"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"},
    {"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"},
    {"key": "g", "title": "Others", "image": "require('./img/m.jpg')"},
]

to this 对此

[
    {"key": "a", "title": "Bangladesh", "image": require('./img/bangladesh.jpg')},
    {"key": "b", "title": "Sports", "image": require('./img/sports.jpg')},
    {"key": "c", "title": "Politics", "image": require('./img/politics.jpg')},
    {"key": "d", "title": "Entertainment", "image": require('./img/entertainment.png')},
    {"key": "e", "title": "Economics", "image": require('./img/economics.jpg')},
    {"key": "f", "title": "Technology", "image": require('./img/technology.jpg')},
    {"key": "g", "title": "Others", "image": require('./img/m.jpg')},
]

Your image source is string. 您的图像源是字符串。 You should eval it. 您应该评估它。

<FlatList horizontal= {true}
  data={newsCategory}
    renderItem={({item}) => (
      <TouchableOpacity
        style={styles.option}
      >
      <Image
        style={styles.imgButton}
        source={eval(item.image)}
      />
      <Text style={styles.buttonText}>{item.title}</Text>
    </TouchableOpacity>
     )
   }
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 JSON 文件获取本地存储的图像的路径 - React Native - Getting the path of an image stored locally from a JSON file - React Native 反应本地JSON图像 - react native json image 我如何从存储在GitHub上的JSON文件中加载数据? - How can I load data from a JSON file which is stored at GitHub in React Native? 从 JSON 文件动态加载图像位置 - (找不到模块...)反应,Next.js - Loading image location from JSON file dynamically - (Cannot find module…) React, Next.js 图片不显示 React native Expo - Image not Showing React native Expo 我尝试从 react native 中的另一个 js 文件导入 JS 文件中的数组数据,并尝试检查 react native 中的数组长度 - i try to import a array data in JS file from another js file in react native , and i try to check the length of the array in react native 如何从 JSON 文件在 React Native 应用程序中渲染图像? - How can I render an image in a React Native application from a JSON file? 为什么来自JSON的图像未在react native中的image标签中呈现? - Why image from JSON is not rendered in image tag in react native? 从存储在github中的json反应本机负载数据 - React native load data from json which stored in github 尝试从远程位置加载json数据时出现错误 - Getting an error when trying to load json data from remote location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM