How to combine multiple inline style objects?

后端 未结 17 1494
难免孤独
难免孤独 2020-11-28 18:54

In React you can clearly create an object and assign it as an inline style. i.e.. mentioned below.

var divStyle = {
          


        
17条回答
  •  醉梦人生
    2020-11-28 19:57

    Actually, there is a formal way to combine and it is like below:

    
    

    But, there is a small issue, if one of them is passed by the parent component and it was created by a combined formal way we have a big problem:

    // The passing style02 from props: [parentStyle01, parentStyle02]
    
    // Now:
    
    

    And this last line causes to have UI bug, surly, React Native cannot deal with a deep array inside an array. So I create my helper function:

    import { StyleSheet } from 'react-native';
    
    const styleJoiner = (...arg) => StyleSheet.flatten(arg);
    

    By using my styleJoiner anywhere you can combine any type of style and combine styles. even undefined or other useless types don't cause to break the styling.

提交回复
热议问题