React Native 如何调用父类的函数 [英] React native how to call functions of parent class

查看:30
本文介绍了React Native 如何调用父类的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 main 类,它包含另一个类的术语

I have a class main which holds another class terms like so

return(
<View>
  <Terms></Terms>
</View>
)

在主类中,我有一个要从条款类调用的函数,这将如何工作?

in the main class I have a function that I want to call from the Terms class, How would that work?

推荐答案

您在主类中有一个函数.在子类道具中传递该函数.假设您在子类中有一个按钮.从 props.onClick 绑定按钮的 onClick.

You have a function in main class. Pass that function in child class props. Suppose you have a button in child class. Bind onClick of button from the props.onClick.

在父类中:

    return(
        <View>
            <Terms onClick={this.onClick}></Terms>
        </View>
    )

        onClick(){
            console.log('I am clicked');
        } 

在子类中:

        <TouchableOpacity onPress={this.props.bind(this.props.onClick)}>
            <Text> Click Me </Text>
        </TouchableOpacity>

这篇关于React Native 如何调用父类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆