我在应用程序中有一个带有 SectionList 的组件,它从本地 json 文件中解析其内容。列表项具有图像和文本,当需要使用 varibale 的图像时,它会导致错误:calls to require expect exactly 1 string literal argument, but this was found: item.logo
。
我的 json 文件
{
"algorithms":[
{"title":"Sorting algorithms","logo":"../logos/sorting.png"},
{"title":"Graph theory","logo":"../logos/graph.png"},
{"title":"Strings","logo":"../logos/strings.png"},
{"title":"Greedy technique","logo":"../logos/greedy.png"},
{"title":"Dynamic programming","logo":"../logos/dp.png"}
],
"datastructures":[
{"title":"Trees","logo":"../logos/tree.png"},
{"title":"Lists","logo":"../logos/list.png"},
{"title":"Tries","logo":"../logos/trie.png"},
{"title":"Stack and Queue","logo":"../logos/stack.png"},
{"title":"Hash Tables","logo":"../logos/hashtable.png"}
]
}
和renderItem
功能
renderItem = ({item}) => {
return(
<TouchableWithoutFeedback onPress={()=>this.props.navigation.navigate('List')}>
<View style={styles.itemHolder}>
<Image resizeMode="center" style={styles.itemLogo} source={require(item.logo)}/>
<View style = {styles.itemNameHolder}>
<Text style = {styles.itemName}>{item.title}</Text>
</View>
<View style={styles.itemPointer}>
<Image style={{width:50,height:50,}} source={require('../icons/right-arrow.png')}/>
</View>
<Divider/>
</View>
</TouchableWithoutFeedback>
);
}
已编辑
js中的函数require
不能动态工作。它的参数必须是字符串。