最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the object in StyleSheet.create?(React Native) - Stack Overflow

programmeradmin6浏览0评论
const styles = StyleSheet.create({

    container : {
        flex : 1,
        backgroundColor : config.getColor('bg'),
    },
    title : {
        marginLeft : 80,
        marginTop : 30,
        height : 35,
        width : 100,
        borderRadius : 17,
        borderWidth : 1,
        borderColor : config.getColor('theme'),
        fontSize : 17,
        color : config.getColor('theme')
    }
});

when I console.log styles.title,I got a number.so how to convert it to a object?

const styles = StyleSheet.create({

    container : {
        flex : 1,
        backgroundColor : config.getColor('bg'),
    },
    title : {
        marginLeft : 80,
        marginTop : 30,
        height : 35,
        width : 100,
        borderRadius : 17,
        borderWidth : 1,
        borderColor : config.getColor('theme'),
        fontSize : 17,
        color : config.getColor('theme')
    }
});

when I console.log styles.title,I got a number.so how to convert it to a object?

Share Improve this question asked Jun 20, 2016 at 8:47 Hai . ZHai . Z 1214 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

You can use the flatten method.

Example:

StyleSheet.flatten(styles.title)

Jean's answer is very good. But I use this pattern to not repeat StyleSheet.flatten every time.

import { StyleSheet } from 'react-native'

const styles = StyleSheet.create({
    container: {
        flex: 1,
        display: 'flex',
        justifyContent: 'space-between',
        padding: 20,
    },
})

export default Object.keys(styles).reduce((newObject, key) => ({ 
  ...newObject, 
  [key]: StyleSheet.flatten(styles[key]) 
}), {})
发布评论

评论列表(0)

  1. 暂无评论