【问题标题】:React inline style - style prop expects a mapping from style properties to values, not a stringReact inline style - style prop 需要从样式属性到值的映射,而不是字符串
【发布时间】:2017-09-08 00:39:00
【问题描述】:

我正在尝试在我的 React 应用程序中设置内联样式。在这种情况下,对于一个跨度:

<span className="myClass" style={{float : 'left', paddingRight : '5px'}} > </span>

React 告诉我:

未捕获的不变违规:style 属性需要从 将属性设置为值,而不是字符串。例如, 使用 JSX 时 style={{marginRight: spacing + 'em'}}。这个 DOM 节点 由 `SentenceView

渲染

我不太清楚这是什么意思。

PS:我尝试了不同的版本,所以我做了paddingRight: 5 以及paddingRight: 5 + 'px' 以及paddingRight : 5px,但我没有任何成功!

【问题讨论】:

    标签: javascript css reactjs


    【解决方案1】:

    使用“styles”道具代替style

    <span className="myClass" style={{float : 'left', paddingRight : '5px'}} > </span>
    

    这是来自 W3Schools 的一个很好的参考,它还向您展示了如何创建具有样式信息的对象,并在 style 属性中引用它: reference for how to style React using CSS

    【讨论】:

    • styles 不是有效的 jsx 属性...这是怎么解决的?
    • 同意上面的评论,尝试了样式但没有用,但如果我这样做了,“样式”它就可以了。不知道为什么选择这个作为答案
    • "styles 不是有效的 jsx 属性" 跟我一样
    • @mohsinulhaq 你在使用 CSS 模块吗?如果是这样,那么您需要className={myStyles},而不是styles={myStyles}。 CSS 模块会自动将您的样式对象转换为 className 字符串。
    • 我将其标记为删除,因为它真的没用,我是通过其他人对这篇文章的引用来到这里的,这绝对是错误的。惊讶地看到这篇文章的票数更高。
    【解决方案2】:

    这是您可以通过 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>
            )
        }
    }
    

    【讨论】:

    • 感谢您的回答。它使用我觉得最熟悉的 React 类模型,因为我使用 create-react-app。
    【解决方案3】:

    有一些方法可以为 React 组件设置样式。

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

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

    1. 使用style={css_object}style={{color: this.props.color}}

    2. 使用className="your-class-name"

    反应 REPL

    https://jscomplete.com/repl

    1 样式对象

    // <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);

    2 类名和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";
    }
    【解决方案4】:

    不要将 {{}} 括在双引号或字符串中

    【讨论】:

      【解决方案5】:

      JSX 和 HTML 是不同的。见the graphic below from Udemy

      在 HTML 中是

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

      在 JSX 中你写

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

      两者的条件内联格式不同。

      【讨论】:

      【解决方案6】:

      JSX 和 html 是不同的东西,我们有一些不同的语法来在 jsx 中添加内联 css 我建议阅读整个文档以便更好地理解 https://www.w3schools.com/react/react_css.asp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-04
        • 2021-11-27
        • 2019-03-02
        • 2017-07-03
        相关资源
        最近更新 更多