Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPress called on load

Tags:

react-native

I have added a function to the onPress method but the function is automatically triggered on loading the app. Am I doing something wrong?

<TouchableHighlight onPress={this.showMenu()}>
   <View></View>
</TouchableHighlight>

showMenu(){
   this.state.showMenu = true;
}
like image 806
Greg Avatar asked Jul 30 '16 10:07

Greg


2 Answers

You are doing it wrong. You should only bind the method on onPress. But you are calling it. Change the onPress as

onPress={this.showMenu.bind(this)}

like image 79
Jagadish Upadhyay Avatar answered Dec 24 '22 02:12

Jagadish Upadhyay


You are actually executing the function from within the onPress directive.

You should use

onPress={this.showMenu}

Without the parenthesis, the function will not trigger at loading, only when the user triggers it.

like image 30
Ernest Jones Avatar answered Dec 24 '22 02:12

Ernest Jones