Diego Bittencourt
Diego Bittencourt

Reputation: 605

How can I set onPress event in a child component?

I have a file called SplashScreen.js with a StackNavigator. Sample code:

enter image description here

Inside my SplashScreen.js I have a component called "Login" and INSIDE Login I have a component called "TouchbleOpacity"

enter image description here

What I need is to change the "onPress" event of my TouchbleOpacity component. So I'll be able to navigate in my Navigator (that are inside my SplashScreen.js). The onPress event should look similar to this: onPress={() => navigation.navigate('TelaCadastrar01')

If there is a better way to change the onPress event of my TouchbleOpacity, please tell me, thanks!

Upvotes: 0

Views: 2803

Answers (1)

Tim
Tim

Reputation: 10709

I don't know if this is what you are looking for, but i'll give it a try:

In your Login Component you do:

//first button
<TouchableOpacity onPress={this.props.onPress} >
    <Text> ... </Text>
</TouchableOpacity> 

//second button 
<TouchableOpacity onPress={this.props.onPressButton2} >
    <Text> ... </Text>
</TouchableOpacity> 

Now you are able to pass any onPress function to your Login Component. e.g.

<Login onPress={() => navigation.navigate('TelaCadastrar01')} onPressButton2={() => console.log('second scene')}/> 

Upvotes: 2

Related Questions