【发布时间】:2021-07-01 16:34:50
【问题描述】:
我在将图像从 JSON 文件渲染到部分列表时遇到了一些问题,即使我对文本数据没有任何问题。
这是我的 JSON 数据:
{
"title" : "Friday",
"data":
[
{
"artist": "artist 1",
"stage": "The Oak",
"Instagram": "insta",
"Spotify": "spot",
"date": "10/8/2021",
"Image": "../assets/art1.jpg",
"Time" : "4:30PM - 5:00 PM"
},
{
"artist": "artist 2",
"stage": "The Oak",
"Instagram": "insta",
"Spotify": "spot",
"date": "10/8/2021",
"Image": "../assets/art2.jpg",
"Time" : "5.00PM - 5:30 PM"
}
]},
我知道您必须使用 require 函数来渲染它,但我似乎无法正确处理它。
这里是章节列表:
<SectionList
sections={data}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (<View style={styles.listItemView}>
<Image style={styles.image} source={require(item.Image)} />
<View>
<Text style={styles.listItem}>{item.artist}</Text>
<Text style={styles.listItemTime} >{item.Time}</Text>
</View>
</View>)}
renderSectionHeader={({ section }) => (
<View style={styles.item}>
<Text style={styles.title}>{section.title}</Text>
</View>)}
/>
欢迎任何帮助。如果我需要添加附加信息,请告诉我!
谢谢!
【问题讨论】:
-
动态导入是一种反模式。您不应动态导入图像。即使你成功地使它工作,图像仍然不会被复制到 bundle 中,因为 react native 会认为这些图像没有被使用。 github.com/benmosher/eslint-plugin-import/blob/v2.22.1/docs/…
-
谢谢@UğurEren 有没有办法正确实现这个效果?
-
没有对每个图像进行硬编码
标签: javascript react-native react-native-flatlist react-native-sectionlist