admin管理员组文章数量:1393120
I'm trying to set the text color of a button in my react-native app via the TouchableOpacity object. I am using the same code for doing this that I have seen in numerous places on line, but it is generating an error for me. I'm trying to figure out what the correct coding of this has to be.
Here is the app ...
import React, {Component} from 'react';
import {StyleSheet, TouchableOpacity, Text, View} from 'react-native';
class App extends Component {
state = {
count: 0,
};
onPress = () => {
this.setState({
count: this.state.count + 1,
});
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button} onPress={this.onPress}>
<Text> style={{color: '#FFFFFF'}}>Hippo counter</Text>
</TouchableOpacity>
<View>
<Text>Hippo count: {this.state.count}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
alignItems: 'center',
backgroundColor: '#DF0000',
padding: 10,
marginBottom: 10
},
});
export default App;
When I run this react-native app, I get the following error:
Render Error
Objects are not valid as a React child (found: object with keys {color}). If you meant to render a collection of children, use an array instead.
Given that this same procedure for setting button text color works in a number of examples that I have found on line, I'm wondering what else I might be doing incorrectly which triggers this error in my specific case.
If I remove style={{color: '#FFFFFF'}}>, from the "Text" attribute of the "TouchableOpacity" object, then the app runs properly. So what must I do in order to change this text color?
I'm trying to set the text color of a button in my react-native app via the TouchableOpacity object. I am using the same code for doing this that I have seen in numerous places on line, but it is generating an error for me. I'm trying to figure out what the correct coding of this has to be.
Here is the app ...
import React, {Component} from 'react';
import {StyleSheet, TouchableOpacity, Text, View} from 'react-native';
class App extends Component {
state = {
count: 0,
};
onPress = () => {
this.setState({
count: this.state.count + 1,
});
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity style={styles.button} onPress={this.onPress}>
<Text> style={{color: '#FFFFFF'}}>Hippo counter</Text>
</TouchableOpacity>
<View>
<Text>Hippo count: {this.state.count}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
button: {
alignItems: 'center',
backgroundColor: '#DF0000',
padding: 10,
marginBottom: 10
},
});
export default App;
When I run this react-native app, I get the following error:
Render Error
Objects are not valid as a React child (found: object with keys {color}). If you meant to render a collection of children, use an array instead.
Given that this same procedure for setting button text color works in a number of examples that I have found on line, I'm wondering what else I might be doing incorrectly which triggers this error in my specific case.
If I remove style={{color: '#FFFFFF'}}>, from the "Text" attribute of the "TouchableOpacity" object, then the app runs properly. So what must I do in order to change this text color?
Share Improve this question edited Mar 14 at 13:41 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Mar 14 at 0:41 HippoManHippoMan 2,3402 gold badges26 silver badges56 bronze badges1 Answer
Reset to default 1There is a small typo here :
<Text> style={{color: '#FFFFFF'}}Hippo counter</Text>
You are closing the Text tag too soon.
Corrected :
<Text style={{color: '#FFFFFF'}}>Hippo counter</Text>
本文标签: cssReactNativeJavascript unable to set button text via TouchableOpacityStack Overflow
版权声明:本文标题:css - React-NativeJavascript: unable to set button text via TouchableOpacity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744678420a2619253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论