1
我希望在按下按鈕時更改其顏色。我嘗試檢查出其他類似的主題,但我找不到解決方案。代碼呈現並且初始按鈕顏色爲紅色,但是當我按下它時,沒有任何反應。通過在React Native上使用onPress更改按鈕顏色
export default class someProgram extends Component {
render() {
var buttonColor = "red";
function changeButtonColor(){
if(this.buttonColor === "red"){
this.buttonColor = "green";
}
else{
this.buttonColor = "red";
}
}
return (
<View style={styles.container}>
<Button
title="Press me!"
color={buttonColor}
onPress={() => {changeButtonColor(buttonColor)}}
/>
</View>
);
}
}
太棒了。完美工作。非常感謝你。 – user3348949