【发布时间】:2019-01-10 19:20:39
【问题描述】:
我在 TouchableOpacity 中有一个 Text 组件,我想根据 var 更改颜色。
这是我的代码:
import React, { Component } from "react";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
var flag = false;
export default class MyTest extends Component {
changeColor() {
flag = true;
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={{ flex: 1, backgroundColor: "#888888", margin: 20 }}
onPress={this.changeColor.bind(this)}
>
<Text style={[{ color: "blue" }, flag ? { color: "red" } : false]}>
One
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#F5FCFF"
}
});
我尝试使用this.state.textColor,但没有结果。我也尝试在样式变量中使用它,但同样不起作用。
有什么想法吗?
【问题讨论】:
标签: javascript android reactjs react-native