开发者问题收集

错误:`style` prop 需要从样式属性到值的映射,而不是 ReactJS 中的字符串

2020-07-23
9715

错误: style prop 需要从样式属性到值的映射,而不是字符串。例如,使用 JSX 时 style={{marginRight: Spacing + 'em'}>

./src/List.js :

import React, {Component} from "react";
import './styles.css';


export default class BlogDetail extends Component{
    render(){
        return(

        <div>

        <section class="blog-details-hero spad set-bg">
        <div class="container">
            <div class="hero-image">
              <div class="hero-text">
                <h1 style="font-size:50px">I am John Doe</h1>
                <p>And I'm a Photographer</p>
              </div>
            </div>
        </div>
        </section>
    </div>

        )
    }
}

./src/styles.css :


.hero-image {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("http://3.128.190.113/media/cardekho-blog/blog-4.jpg");
  height: 50%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}

.hero-text {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
}

3个回答

style 属性更改为对象和 camelCased 属性:

<h1 style={{fontSize: `50px`}}>I am John Doe</h1>

请参阅 React 文档中的 style

The style attribute accepts a JavaScript object with camelCased properties rather than a CSS string.

Dennis Vash
2020-07-23

如错误消息所示,style 属性需要符合规范。有关更多信息,请参阅 文档

无论如何,这应该可以解决您的问题 -

<h1 style={{fontSize: '50px'}}>I am John Doe</h1> 
Karthik Rana
2020-07-23

It is so simple you used this :

<h1 style="font-size:50px">I am John Doe</h1

Use this :

<h1 style={{font-size:"50px"}}>I am John Doe</h1>
Saif Ali
2021-07-01