使用按钮更改文本的颜色
Using a button to change the color of text
我正在尝试创建一个简单的 React Native 应用程序:
- 当用户输入名称时呈现 "Hello, [name]!"(这部分有效)
- 按下按钮时更改 "Hello, [name]!" 文本颜色。
关于我应该如何处理这件事有什么想法吗?
我给 this
一个黑色的初始状态,但它似乎什么也没做。
我想要发生的是在单击红色按钮时触发 makeRed,这将使文本变为红色。完成此工作后,我将添加更多颜色按钮。
谢谢!
请参阅下面的 App.js 代码。所有其他文件均保留其默认状态。
import React, { Component } from 'react';
import {
AppRegistry,
Platform,
StyleSheet,
Text,
TextInput,
View,
Button
} from 'react-native';
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
text: 'World',
color: 'black'
};
}
makeRed = () => {
this.setState({
color: 'red'
});
}
render() {
return (
<View style={styles.container}>
<Text style={[styles.welcome, {color: undefined}]}>
Hello, {this.state.text}!
</Text>
<TextInput
style={styles.instructions}
placeholder="Enter a name here!"
onChangeText={(text) => this.setState({text})}
/>
<Button
title='⬤'
onPress={this.makeRed}
color='red'
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 40,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
这是应用程序的屏幕截图以供参考:
app screenshot
在 Text
组件的样式中,color
未被引用为状态的 属性。试试这个作为 Text
元素:
<Text style={{color: this.state.color, ...styles.welcome}}>
Hello, {this.state.text}!
</Text>
并且,makeRed
需要在构造函数中绑定上下文,否则 this.setState
将是未定义的。像这样(在 super(props)
下):
this.makeRed = this.makeRed.bind(this)
你需要做的就是改变这个:
style={[styles.welcome, {color: undefined}]}
到
style={[styles.welcome, {color : this.state.color }]}
请检查:WORKING DEMO
我正在尝试创建一个简单的 React Native 应用程序:
- 当用户输入名称时呈现 "Hello, [name]!"(这部分有效)
- 按下按钮时更改 "Hello, [name]!" 文本颜色。
关于我应该如何处理这件事有什么想法吗?
我给 this
一个黑色的初始状态,但它似乎什么也没做。
我想要发生的是在单击红色按钮时触发 makeRed,这将使文本变为红色。完成此工作后,我将添加更多颜色按钮。
谢谢!
请参阅下面的 App.js 代码。所有其他文件均保留其默认状态。
import React, { Component } from 'react';
import {
AppRegistry,
Platform,
StyleSheet,
Text,
TextInput,
View,
Button
} from 'react-native';
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
text: 'World',
color: 'black'
};
}
makeRed = () => {
this.setState({
color: 'red'
});
}
render() {
return (
<View style={styles.container}>
<Text style={[styles.welcome, {color: undefined}]}>
Hello, {this.state.text}!
</Text>
<TextInput
style={styles.instructions}
placeholder="Enter a name here!"
onChangeText={(text) => this.setState({text})}
/>
<Button
title='⬤'
onPress={this.makeRed}
color='red'
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 40,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
这是应用程序的屏幕截图以供参考: app screenshot
Text
组件的样式中,color
未被引用为状态的 属性。试试这个作为 Text
元素:
<Text style={{color: this.state.color, ...styles.welcome}}>
Hello, {this.state.text}!
</Text>
并且,makeRed
需要在构造函数中绑定上下文,否则 this.setState
将是未定义的。像这样(在 super(props)
下):
this.makeRed = this.makeRed.bind(this)
你需要做的就是改变这个:
style={[styles.welcome, {color: undefined}]}
到
style={[styles.welcome, {color : this.state.color }]}
请检查:WORKING DEMO