React inline style - style prop expects a mapping from style properties to values, not a string

后端 未结 6 1071
北荒
北荒 2020-12-13 08:03

I am trying to set inline styles in my React application. In this case, for a span:



        
相关标签:
6条回答
  • 2020-12-13 08:37

    This is the way how you can define and use inline style with react.

    /**
     * Style definitions.
     */
    const STYLE = {
        infoColor: {
            color: 'green'
        },
        warningColor: {
            color: 'orange'
        },
        errorColor: {
            color: 'red'
        }
    };
    
    /**
     * Component
     */
    class Welcome extends React.Component {
    
        /**
         * Rendering into the DOM.
         */
        render() {
            return (
                <div>
                    <h2 style={STYLE.infoColor}>Welcome!</h2>
            )
        }
    }
    
    0 讨论(0)
  • 2020-12-13 08:38

    don't wrap the {{}} in double quotes or string

    0 讨论(0)
  • 2020-12-13 08:38

    JSX and html are different things and we have a little different syntax to add inline css in jsx I would recommend to go through whole documentation for better understanding https://www.w3schools.com/react/react_css.asp

    0 讨论(0)
  • 2020-12-13 08:45

    There some ways to set style for React Components.

    https://facebook.github.io/react/docs/context.html

    https://github.com/facebookincubator/create-react-app

    1. using className="your-class-name"

    2. using style={css_object} or style={{color: this.props.color}}

    React REPL

    https://jscomplete.com/repl

    1 className & stylesheet.css

    import './styles.css';
    
    /*
    .classname-color{
        color: "red";
        background: "#0f0";
    }
    */
    
    
    const BTN = (props) => {
        return (
            <div>
                My name is <button>{props.name}</button>
                <hr/>
                I'm <span className="classname-color">{props.age}</span> yeas old!
            </div>
        );
    };
    
    const infos = {
        name: "xgqfrms",
        age: 23
    };
    
    ReactDOM.render(<BTN {...infos} />, mountNode);
    .classname-color{
        color: "red";
        background: "#0f0";
    }

    2 style Object

    // <span style={styles}>
    
    const styles = {
        color: "red",
        background: "#0f0",
        fontSize: "32px"
    };
    
    const BTN = (props) => {
        return (
            <div>
               My name is <button>{props.name}</button>
               <hr/>
               I'm <span style={styles}>{props.age}</span> yeas old!
            </div>
        );
    };
    
    const infos = {
        name: "xgqfrms",
        age: 23
    };
    
    ReactDOM.render(<BTN {...infos} />, mountNode);
    
    
    
    
    // <span style={{color: styles.color}}>
    
    const styles = {
        color: "red",
        background: "#0f0",
        fontSize: "32px"
    };
    
    const BTN = (props) => {
        return (
            <div>
               My name is <button>{props.name}</button>
               <hr/>
               I'm <span style={{color: styles.color}}>{props.age}</span> yeas old!
            </div>
        );
    };
    
    const infos = {
        name: "xgqfrms",
        age: 23
    };
    
    ReactDOM.render(<BTN {...infos} />, mountNode);

    0 讨论(0)
  • 2020-12-13 08:55

    Use "styles" prop instead of style

    <span className="myClass" style={{float : 'left', paddingRight : '5px'}} > </span>
    
    0 讨论(0)
  • 2020-12-13 08:57

    JSX AND HTML are different:

    In HTML it is

    <div style="background-color: red;"></div>
    

    In JSX you write

    <div style={{ backgroundColor: 'red' }}></div>
    

    Conditional inline formatting are different in both.

    0 讨论(0)
提交回复
热议问题