【发布时间】:2021-07-30 12:03:36
【问题描述】:
我使用 redux 创建了一个计数器应用程序我想在值为 0 时更改颜色,并在值为 1 时再次更改。我为此创建了一个函数,当我使用 console.log 它显示正确的颜色名称但是当我以显示未定义的背景颜色名称调用函数时,它不显示。 (我不想为此使用三元运算符)
class counterApp extends Component {
state={
maincolor: '',
}
fun(){
if (this.props.counter == 0) {
this.state.maincolor = dark.bg
}
else {
this.state.maincolor = light.bg
}
}
render() {
return (
<View style={{
justifyContent: 'center',
alignItems: 'center', top: '45%',
backgroundColor: this.fun()
}}>
【问题讨论】:
-
尝试在函数中添加return?
fun () { if (this.props.counter == 0) { return dark.bg } } -
这与
python标签有什么关系? -
您对此有什么问题吗?
标签: reactjs react-native react-redux react-native-stylesheet