Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  129] [ 4]  / answers: 1 / hits: 47063  / 7 Years ago, fri, august 25, 2017, 12:00:00

I'm trying to bind the function handleClick to my button onPress. But it's not working. When I refresh the page, I get the alert without clicking on the button and after I close the alert and click on the button, nothing happens.



My Code is:



class ActionTest extends Component {
constructor(props) {
super(props);
this.state = {
thename: 'somename'
};
}

handleClick(){
alert('Button clicked!');
}

render(){
return(
<View>
<Button
onPress={this.handleClick()}
title=Click ME
color=blue
/>
</View>
);
}
}


I'm also getting the warning :



enter



What am I doing wrong?


More From » reactjs

 Answers
32

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.


[#56656] Tuesday, August 22, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;