Adam Katz
Adam Katz

Reputation: 6962

React native how to call functions of parent class

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?

Upvotes: 2

Views: 3346

Answers (1)

Jagadish Upadhyay
Jagadish Upadhyay

Reputation: 1264

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.

In parent Class:

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

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

In child class:

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

Upvotes: 6

Related Questions