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)});
}
添加回答
举报