【发布时间】:2020-03-10 19:37:04
【问题描述】:
React Native:当图像路径数组从 fetchapi 动态获取时,获取图像的替代方法需要工作
尝试了以下方法并意识到 require 总是需要引号('')中的固定路径,这在已知静态图像路径时效果很好。你应该是映射从 fetchapi 接收到的图像的替代方法。
source={require('./assets/images/'+{item.image_path})}
Example fetch api json response is: [{user_name: 'DynamicUser1', image_path: 'DynamicUser1.jpg'},{user_name: 'TestUser1', image_path: 'TestUser1.jpg'}... ]
需要其他方法来获取使用从 fetch api 正确选择的动态图像路径渲染的图像。
userData = this.state.resultsUserData.map((item) => {
return (
<View key={item.user_name} style={ styles.container }>
<Text style={styles.title}>
{item.user_name}
</Text>
<TouchableOpacity goBack={()=>this.backAction} onPress={()=>this.homepage()}>
<Image key={item.image_path}
source={require('./assets/images/'+{item.image_path})}
</TouchableOpacity>
</View>
)
}
【问题讨论】:
标签: react-native