Why can't we check the style attribute of the app responsible for the response?

I wanted to check the white color of the item as below,

if(styles.background=='white')
console.log("ok")

console.log(styles.background=='white') --> was false [1]

why does [1] return false?

+2
source share
2 answers

1 - In your case, styles are a StyleSheet object.

You need to use the StyleSheet.flatten function as shown below:

 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

2 - handle the component style:

var flattenStyle = require('flattenStyle');
var backgroundColor = flattenStyle(this.props.style).backgroundColor;

More details here:

https://facebook.imtqy.com/react-native/docs/stylesheet.html

and

fooobar.com/questions/624914 / ...

+4
source

, .
(: , , .)

:

var styleObj = StyleSheet.flatten(styles.container)

:

var styleObj = StyleSheet.flatten(styles[1].container)

dict :

console.log(styleObj)
+3

Source: https://habr.com/ru/post/1671995/


All Articles