How to combine multiple inline style objects?

后端 未结 17 1439
难免孤独
难免孤独 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:48

    To Expand on what @PythonIsGreat said, I create a global function that will do it for me:

    var css = function(){
        var args = $.merge([true, {}], Array.prototype.splice.call(arguments, 0));
        return $.extend.apply(null, args);
    }
    

    This deeply extends the objects into a new object and allows for a variable number of objects as parameters. This allows you to do something like this:

    return(
    <div style={css(styles.base, styles.first, styles.second,...)} ></div>
    );
    
    var styles = {
      base:{
        //whatever
      },
      first:{
        //whatever
      },
      second:{
        //whatever
      }
    }
    
    0 讨论(0)
  • 2020-11-28 19:49

    Array notaion is the best way of combining styles in react native.

    This shows how to combine 2 Style objects,

    <Text style={[styles.base, styles.background]} >Test </Text>
    

    this shows how to combine Style object and property,

    <Text style={[styles.base, {color: 'red'}]} >Test </Text>
    

    This will work on any react native application.

    0 讨论(0)
  • 2020-11-28 19:49

    So basically I'm looking at this in the wrong way. From what I see, this is not a React specific question, more of a JavaScript question in how do I combine two JavaScript objects together (without clobbering similarly named properties).

    In this StackOverflow answer it explains it. How can I merge properties of two JavaScript objects dynamically?

    In jQuery I can use the extend method.

    0 讨论(0)
  • 2020-11-28 19:51

    You can do this with Object.assign().

    In your example, you would do:

    ReactDOM.render(
        <div style={Object.assign(divStyle, divStyle2)}>
            Hello World!
        </div>,
        mountNode
    );
    

    That will merge the two styles. The second style will replace the first if there are matching properties.

    As Brandon noted, you should use Object.assign({}, divStyle, divStyle2) if you want to reuse divStyle without the fontSize applied to it.

    I like to use this to make components with default properties. For example, here's a little stateless component with a default margin-right:

    const DivWithDefaults = ({ style, children, ...otherProps }) =>
        <div style={Object.assign({ marginRight: "1.5em" }, style)} {...otherProps}>
            {children}
        </div>;
    

    So we can render something like this:

    <DivWithDefaults>
        Some text.
    </DivWithDefaults>
    <DivWithDefaults className="someClass" style={{ width: "50%" }}>
        Some more text.
    </DivWithDefaults>
    <DivWithDefaults id="someID" style={{ marginRight: "10px", height: "20px"}}>
        Even more text.
    </DivWithDefaults>
    

    Which will give us the result:

    <div style="margin-right:1.5em;">Some text.</div>
    <div style="margin-right:1.5em;width50%;" class="someClass">Some more text.</div>
    <div style="margin-right:10px;height:20px;" id="someID">Even more text.</div>
    
    0 讨论(0)
  • 2020-11-28 19:51

    For ones that looking this solution in React, If you want to use the spread operator inside style, you should use: babel-plugin-transform-object-rest-spread.

    Install it by npm module and configure your .babelrc as such:

    {
      "presets": ["env", "react"],
      "plugins": ["transform-object-rest-spread"]
    }
    

    Then you can use like...

    const sizing = { width: 200, height: 200 }
     <div
       className="dragon-avatar-image-background"
       style={{ backgroundColor: blue, ...sizing }}
      />
    

    More info: https://babeljs.io/docs/en/babel-plugin-transform-object-rest-spread/

    0 讨论(0)
  • 2020-11-28 19:53

    Need to merge the properties in object. For Example,

    const boxStyle = {
      width : "50px",
      height : "50px"
    };
    const redBackground = {
      ...boxStyle,
      background: "red",
    };
    const blueBackground = {
      ...boxStyle,
      background: "blue",
    }
    
    
    
    
     <div style={redBackground}></div>
     <div style={blueBackground}></div>
    
    0 讨论(0)
提交回复
热议问题