繁体   English   中英

如何在没有箭头和没有绑定的情况下在 TouchableOpacity 的 onPress 方法中传递值 function - **JSX 道具不应使用箭头功能**

[英]How to pass value in onPress method of TouchableOpacity without arrow and without bind function - **JSX props should not use arrow functions**

我在 React Native 项目的视图中有一个 TouchableOpacity,它有 onPress 方法。 我不想在 onPress 方法中使用箭头 function 和绑定 function,因为这每次都会创建一个新的 function。 我想在 onPress 方法中传递参数。 下面是相同的代码 -

<TouchableOpacity
        activeOpacity={0.9}
        style={styles.touchOfferItem}
        onPress={() => this.gotoOfferDetail(item)}
        accessibilityLabel={`Test`}
        testID={'Test'}
        accessible={false}>
</TouchableOpacity>
gotoOfferDetail (offerData:IOfferDetailItem) {
    console.log(offerData.title)
  }

我不想在箭头 function 中使用 onPress ,因为它给我JSX props should not use arrow functions 任何人都可以帮助我们解决这种情况并提供任何解决方案吗?

非常感谢任何帮助。

我搜索了很多东西但没有找到任何解决方案。

您需要一个 function,它返回另一个 function,如下所示:

gotoOfferDetail (offerData: IOfferDetailItem) {
    return () => console.log(offerData.title)
}
<TouchableOpacity
        activeOpacity={0.9}
        style={styles.touchOfferItem}
        onPress={this.gotoOfferDetail(item)}
        accessibilityLabel={`Test`}
        testID={'Test'}
        accessible={false}>
</TouchableOpacity>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM