2 回答

TA貢獻1853條經驗 獲得超6個贊
export default class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {color: "#000000"}
}
render() {
let color = this.state.color;
return (
<View>
<Button
onPress = {this.changecolor}
title = "This button changes color when pressed"
color = {color}/>
</View>
numbers = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
this.random = this.numbers[Math.floor(Math.random() * this.numbers.length)];
changecolor() {
this.replaceState({color: String("#"+random+random+random+random+random+random)});
}
}

TA貢獻1811條經驗 獲得超6個贊
changecolor甚至是班級成員HomeScreen?看起來不像,考慮到它是用聲明的function并且沒有解析錯誤對你大喊大叫。讓它成為一個方法,更好的是,一個箭頭函數:
changecolor = () => {
this.replaceState({color: String("#"+random+random+random+random+random+random)});
}
添加回答
舉報