在您的情况下,样式是一个 StyleSheet 对象。
您需要使用样式表.展平功能如下:
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
var styleObj = StyleSheet.flatten([styles.container])
console.warn(styleObj.backgroundColor==='#F5FCFF') //=>true
要处理组件的 prop 样式,您可以按如下方式使用它:
let backgroundColor = Stylesheet.flatten(this.props.style).backgroundColor;
您可以在这里找到该函数的源代码:
https://github.com/facebook/react-native/blob/master/Libraries/StyleSheet/flattenStyle.js https://github.com/facebook/react-native/blob/master/Libraries/StyleSheet/flattenStyle.js
来源和更多详细信息请参见此处:
https://facebook.github.io/react-native/docs/stylesheet.html https://facebook.github.io/react-native/docs/stylesheet.html
https://stackoverflow.com/a/35233409/1979861 https://stackoverflow.com/a/35233409/1979861