React-Native Button onPress not working
You are calling handleClick when it renders as you have onPress={this.handleClick()}
try onPress={this.handleClick}
instead, to pass it the function as a callback.
When react-native detects a click event and wants to notify you, it calls the onPress
prop as a function. so you have to give a function to onPress
prop like:
onPress={this.handleClick}
this connects onPress
to handleClick
method. but if you want to call other methods in handleClick
and you need "this
object" you can pass the handleClick
method like this:
onPress={this.handleClick.bind(this)}
Good luck
First you define your click handler as an arrow function. In this way you don't need to bind the function anymore. Your function will be like this:
handleClick = () => {
alert('Button clicked!');
}
Then use this function in the <Button>
tag like this:
<Button
onPress={this.handleClick}
title="Click ME"
color="blue"
/>