【问题标题】:Parent and child onpress function父子onpress功能
【发布时间】:2020-08-28 20:21:36
【问题描述】:

我希望一个组件应该接受来自父级和子级的 onpress 事件,如下所示:

import { TouchableOpacity } from "react-native";

function MyComponent(){
    testFunc1 = ()=>{
        alert("test");
    }

    testFunc2 = ()=>{
       alert("test");
    }

    return(
       <TouchableOpacity onPress={this.testFunc1}>
       <Button onPress={this.testFunc2} title="Call Test"  />
        </TouchableOpacity>
    );
}

【问题讨论】:

    标签: react-native event-bubbling


    【解决方案1】:

    您可以将点击动作的道具传递给孩子

        import { TouchableOpacity,Text} from "react-native";
        Const childComponent=(props) =>{
         Const handleAction={
          props.clickAction();
          alert("child action")
        }
        return(
          <TouchableOpacity onPress={handleAction}> 
          <Text>touchableButton<Text>
          </TouchableOpacity>
           ) 
           }
    

    【讨论】:

    • 我有 2 个不同的功能。我已经更新了我的问题。
    • 好的,这意味着你可以在你的孩子中调用单独的函数,父函数作为回调,否则你想加载一个意味着你可以使用条件
    • 我有父组件和子组件都有 onpress 事件。当单击子父级时仅调用。孩子没叫
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多