arrays - 如何在 React Native 的 .map 中调用类函数
问题描述
我将在模板中调用类的函数。这就是我所做的。
class recommendFeedback extends Component<{}>{
starImgFromRating(tip) { // this function should be called but not.
// ... some operation with tip.
return result
}
render(){
return(
<View style = {styles.container}>
{
this.props.venue.tips.map(function(item) {
return <View>
<Thumbnail source = {()=>this.starImgFromRating(item)} />
</View>
})
}
</View>
)
}
}
但是效果不好。
解决方案
应该是source = {this.starImgFromRating(item)}
因为您需要starImgFromRating
在渲染期间评估Thumbnail
要使用的组件的结果。
将函数作为 prop 值传递通常用于事件处理程序。
您还应该将箭头函数用于map
:.map((item) => {
以避免绑定this
到该函数。
推荐阅读
- python - 使用 pandas 删除值中不包含字符串的行
- java - 拦截 monitorEnter - 生成的字节码无法 JIT 编译
- rabbitmq - Spring AMQP RPC使用者如何确定发布者通道是否已关闭
- javascript - 如何在新函数中使用带有 OnChange 的文本框中的值?
- model-view-controller - 剑道 UI MVC 下拉列表
- spring - 如何从java代码配置spring boot服务器(初始化时)?
- sql - SQL 使用 oracle SQL 上的查询测试外键
- excel - 更改数据透视表源时出现无效的过程错误?
- django - 在 Django url 路径中使用变量名而不是整数
- vba - 列表中有新段落时如何获取列表级别