[英]React-Native Button On Press doesn't work
我無法將onPress方法綁定到我的JSX按鈕。 我已經嘗試了無數種不同的解決方案,但是沒有一個起作用。
onPress方法:
class ScreenEnterPlayerNames extends Component {
constructor(props) {
super(props);
}
static navigationOptions = {
title: 'Player Names',
};
onPressStartTheGame = () => {
console.log("Pushed button Start the Game!")
//dispatch({ type: ADD_PLAYER_NAME, playerNumber: 5, playerName: "Sandro" })
}
按鈕:
return (
<View style={styles.viewMain}>
<View style={styles.viewTxt}>
<Text style={styles.TxtHeading}>
Please enter the names in sequence.
</Text>
</View>
<ScrollView style={styles.viewTextBox}>
{textBoxes}
</ScrollView>
<View style={styles.viewButton}>
<Button
title='Start the Game!'
onPress={() => this.onPressStartTheGame}
/>
</View>
</View>
);
}
}
我也嘗試了以下方法:
onPress={this.onPressStartTheGame}
onPress={this.onPressStartTheGame()}
onPress={this.onPressStartTheGame.bind(this)}
onPress={() => this.onPressStartTheGame.bind(this)}
我試圖將功能更改為:
onPressStartTheGame() {...
因此,我很確定還有其他問題,但我不知道是什么原因。
謝謝! :-)
嘗試這種方法
onPressStartTheGame(){
console.log("Pushed button Start the Game!")
//dispatch({ type: ADD_PLAYER_NAME, playerNumber: 5, playerName: "Sandro" })
}
並稱它為
onPress={this.onPressStartTheGame.bind(this)}
在這里,您可以仔細檢查並嘗試在沙箱中的行為。 https://facebook.github.io/react-native/docs/handling-touches.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.