【发布时间】:2020-05-06 20:29:55
【问题描述】:
我不明白为什么组件在 setState 之后没有被重新渲染。我认为绑定 onPress 方法会有所帮助,但没有。 当我点击 TouchableHighlight 时,它会改变 View backgroundColor,但如果我再次点击它就不会再改变了。
class Item extends Component{
state={
currentColor:"#FFFFF"
}
onClick() {
console.log(this.state.currentColor)
if (this.state.currentColor=='#FFFFF'){
color='green'
} else {
color='#FFFFF'
}
this.setState({currentColor:color})
}
render(){
return (
<TouchableHighlight onPress={ this.onClick.bind(this) } >
<View style={[styles.item, {backgroundColor: this.state.currentColor}]}>
<Text style={styles.title}>{this.props.title}</Text>
</View>
</TouchableHighlight>
);
}
}
【问题讨论】:
-
#FFFFF不是有效的颜色代码,请尝试#FFFFFF -
不确定是否相关,但
color未在您的函数范围内定义。
标签: javascript reactjs react-native