问题描述
我正在使用React Native创建一个应用程序,当我单击GO
文本时,它更改为字母X
。但是,当我再次单击GO
时,我还需要显示X
文本。
这是下面的代码
this.state = {
textValue:"You're Offile",cancel:'X',textGo:"GO",}
onChange = () => {
this.setState({
textValue: 'Finding Rides',textGo:"X"
});
};
<TouchableOpacity onPress={() => this.onChange()}>
<Text
//onPress={() => this.onChange()}
style={{
fontSize:30,fontWeight:"bold",color:"white"
}}>{this.state.textGo}</Text>
</TouchableOpacity>
解决方法
您可以使用onChnage方法检查条件并根据其更新状态。
onChange = () => {
if(this.state.textGo === "X")
{
this.setState({
textValue: 'You're Offile',textGo:"GO"
});
}
else
{
this.setState({
textValue: 'Finding Rides',textGo:"X"
});
}
};