简体   繁体   English

我可以在React Native上为this.props.children添加额外的回调吗?

[英]Can I add extra callback to this.props.children on React Native?

Let's say this.props.children[0] is TouchableOpacity which is from parent. 假设this.props.children [0]是来自父级的TouchableOpacity。

And I want add extra callback to it in HERE. 我想在这里添加额外的回调。 Is what I want possible? 我想要什么? I can't find any solution for this. 我找不到任何解决方案。

Thanks. 谢谢。

export default class NewClassXXX extends React.Component{

    render()
    {
        newchildren = React.Children.map(this.props.children, (child) => {
           //child is TouchableOpacity class in my code ***
           //I want to add LongPress callback by coding HERE. (like below)
           child.onLongPress = () => console.log('SUCESSS!!!'); //this code does not work.
           return child;
        }
        return {newchildren}
    }

Use React.cloneElement method to achieve this like this 使用React.cloneElement方法来实现这一点

export default class NewClassXXX extends React.Component{

    render()
    {
        newchildren = React.Children.map(this.props.children, (child) => {
           return React.cloneElement(child, {onLongPress: () => console.log('SUCESSS!!!')})
        }
        return {newchildren}
    }

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

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