我試圖將函數綁定到我的按鈕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觸發時運行該函數 –