react-native - 如何在 onPress 中获取元素键而不在 React Native 中绑定
问题描述
我们已经知道 AirBnB 风格指南不鼓励在渲染函数 ( source ) 中使用 .bind( )。如此处所述:
JSX 道具中的绑定调用或箭头函数将在每个渲染上创建一个全新的函数。这对性能不利,因为它会导致垃圾收集器被调用的方式超出必要的程度。
这是我们以前做的,将参数传递给 bind
renderSingleItem = ({item}) => <TouchableOpacity onPress={func.bind(this, item.id)}>
return <FlatList data={data} renderItem={this.renderSingleItem}/>
这似乎是唯一的方法,因为 React Native 中的 onPress 参数不包含像 ReactJS 那样的目标对象。
反应JS:
typeof event.taget === 'object'
反应原生:
typeof event.taget === 'number'
有更好的方法吗?
解决方案
You can use,
renderSingleItem = ({item}) => <TouchableOpacity onPress={() =>
this.func(item.id)
}>
推荐阅读
- flutter - 由于这个错误,使用 intellij idea 无法构建
- python - 在 Python 中创建没有 for 循环的内核矩阵
- python - Sympy 演算:在评估 sympy 表达式时,python 输出 log(2) 和 e^0.3 作为符号,而不是计算它们的值
- python - 如何将 keras 调谐器与 keras 功能 api 模型一起使用
- javascript - 如何在本机反应中创建带有声音的警报?
- windows - Windows RPC - 如何在服务器中检索客户端指定的预期主体名称?
- flutter - Flutter Provider:无法从提供者的列表中删除项目
- python - 简化代码以查找最后一次出现的值
- bitrix - 将特定时间添加到截止日期
- bash - 在预提交挂钩期间,npm 包在自定义 bash 脚本中不起作用