【发布时间】:2017-08-25 14:26:29
【问题描述】:
我正在尝试将函数 handleClick 绑定到我的按钮 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时运行该函数
标签: javascript reactjs react-native