我试图将函数绑定到我的按钮onPress
。但它不起作用。当我刷新页面时,无需点击按钮即可获得警报,在关闭警报并单击按钮后,什么都不会发生。React-Native按钮onPress不工作
我的代码是:
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>
);
}
}
我也得到警告:
我在做什么错?
要调用的handleclick功能,当组件呈现你想要做onPress = {this.handleClick}'这样这将是一个回调,并且只会在onpress触发时运行该函数 –