簡體   English   中英

我可以在React Native上為this.props.children添加額外的回調嗎?

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

假設this.props.children [0]是來自父級的TouchableOpacity。

我想在這里添加額外的回調。 我想要什么? 我找不到任何解決方案。

謝謝。

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}
    }

使用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