简体   繁体   English

如何导入 CSS Module React 和 JSX? ( 错误 )

[英]How to Import CSS Module React and JSX? ( Error )

IN my react application I want to locally style on of my components.在我的 React 应用程序中,我想在我的组件上设置本地样式。
For that I'm using the yourCSS.module.css convention.为此,我使用yourCSS.module.css约定。

In myComponent.jsx I then import that CSS-Module:myComponent.jsx我然后导入该 CSS 模块:

import React, { Component } from 'react';
import styles from './startPage.module.css' //loading the css moduleenter code here

class StartPage extends Component {
    state = {  }
    render() { 
        return ( 
           <div className="container">
               <div className="row">    {/*Row 1 - contains title*/} 
                    <div style className="col-md-6 offset-md-3">
                        <p styles={styles.boilerplate}>some boilterplate text</p>
                    </div>
               </div>

               <div className="row">    {/*Row 2 - contains buttons*/} 

               </div>
           </div> 
         );
    }
}

export default StartPage;

React then throws the following error in my browser: React 然后在我的浏览器中抛出以下错误:

Error: The `style` prop expects a mapping from style properties to values, not a string.
For example, style={{marginRight: spacing + 'em'}} when using JSX.

My css currently contains我的 css 目前包含

.boilerplate {
    background-color:#000000;
    border: black dotted;
} 

Any help would be appreciated!任何帮助,将不胜感激!

You should use styles prop instead of style您应该使用样式道具而不是样式

<div style className="col-md-6 offset-md-3">

</div>

Or just remove style prop.或者只是删除样式道具。 The value is empty!值为空!

I think it is a typo:我认为这是一个错字:

// remove style prop or it will be converted to style={true}
<div className="col-md-6 offset-md-3">
// use "style" prop instead of "styles"
  <p style={styles.boilerplate}>some boilterplate text</p>
</div>

You can find a documentation on how to use inline css in react here.您可以在此处找到有关如何在 react 中使用内联 css 的文档

Your first choice should be to use the ClassName to reference classes defined in an external CSS stylesheet您的首选应该是使用 ClassName 来引用在外部 CSS 样式表中定义的类

import React, { Component } from 'react';
import './startPage.module.css' //loading a css file here

class StartPage extends Component {
state = {  }
render() { 
    return ( 
       <div className="container">
           <div className="row">    {/*Row 1 - contains title*/} 
                <div className="col-md-6 offset-md-3"> // remove style
                    <p className='boilerplate'>some boilterplate text</p> // add boilerplate as ClassName
                </div>
           </div>

           <div className="row">    {/*Row 2 - contains buttons*/} 

           </div>
       </div> 
     );
}
}

export default StartPage;

But if you would like to use the style attribute it works like this:但是如果你想使用 style 属性,它的工作方式是这样的:

import React, { Component } from 'react';


const divstyles = {
    backgroundColor: '#000000',
    border: 'red dotted'
}

class StartPage extends Component {
state = {  }
render() { 
    return ( 
       <div className="container">
           <div className="row">    {/*Row 1 - contains title*/} 
                <div className="col-md-6 offset-md-3">
                <p style={divstyles}>some boilterplate text</p>
                </div>
           </div>

           <div className="row">    {/*Row 2 - contains buttons*/} 

           </div>
       </div> 
     );
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM