React native 如何在设置状态时比较react native中的颜色对象
有人能帮我比较一下react native中的颜色对象吗。 通常在swift中,我会这样做:React native 如何在设置状态时比较react native中的颜色对象,react-native,conditional,state,React Native,Conditional,State,有人能帮我比较一下react native中的颜色对象吗。 通常在swift中,我会这样做: if aColorObj == UIColorClass.white{ aColorObj = UIColorClass.gray } 如何在下面的react native中执行类似操作: onPressLearnMore() { this.setState = { /* how to write this: if mbackgroundColor == '
if aColorObj == UIColorClass.white{
aColorObj = UIColorClass.gray
}
如何在下面的react native中执行类似操作:
onPressLearnMore() {
this.setState = {
/*
how to write this:
if mbackgroundColor == 'white'{
mbackgroundColor = 'red'
}else if mbackgroundColor == 'gray'{
mbackgroundColor = 'white'
}
*/
};
}
谢谢:)这里最简单的解决方案也是,将初始背景色保存在状态中,使用颜色对象来比较颜色
const Colors = {
Grey: '#DCDCDC',
White: '#FFFFFF',
Blue: '#0000FF',
Black: '#000000',
};
state = {
backgroundColor: Colors.White,
};
<View
style={[
styles.container,
{ backgroundColor: this.state.backgroundColor }, // set background color here from state
]}>
checkBackgroundColor = () => {
if (this.state.backgroundColor === Colors.Blue) {
console.log("It's blue");
this.setState({
backgroundColor: Colors.White,
});
}
....
};