董力 發表於 2019-8-9 11:34:00

react react使用css

<h2><span style="color: rgba(255, 0, 0, 1)">现在使用脚手架(react-create-app)的项目不需要在在css文件后面添加module就会自动为其添加模块化后缀</span></h2>
<p>在react 中使用css有以下几种方法</p>
<p>第一种全局使用</p>
<p>app.js</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import Router from </span>"./router"<span style="color: rgba(0, 0, 0, 1)">

import </span>'./App.css'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> App() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div className="App"&gt;
            &lt;div className='head'&gt;&lt;span&gt;app&lt;/span&gt;&lt;/div&gt;
            &lt;Router/&gt;
      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">    );
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> App;</pre>
</div>
<p>直接在入口文件引入,我这里是app.js 引入或css文件会作用至所有的组件,适合引入全局样式</p>
<p>第二种使用 组件使用css</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import </span>'./home.css' <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">直接引入css文件</span>
<span style="color: rgba(0, 0, 0, 1)">class Home extends React.Component {
    constructor(props) {
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
            date: </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime()
      }
    }

    componentDidMount() {
      </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> f(){
            console.log(</span>"ffff"<span style="color: rgba(0, 0, 0, 1)">);
      }
      console.log(</span><span style="color: rgba(0, 0, 255, 1)">false</span>||<span style="color: rgba(0, 0, 0, 1)">f())
    }

    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;div&gt;
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在jsx中使用</span>
                &lt;div className='head'&gt;
                  &lt;span&gt;主页&lt;/span&gt;
                &lt;/div&gt;
                &lt;div&gt;&lt;span&gt;{<span style="color: rgba(0, 0, 255, 1)">this</span>.date}&lt;/span&gt;&lt;/div&gt;
            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">      )
    }
}

export {Home}</span></pre>
</div>
<p>这种方法和第一种没有太大的区别都是作用在全局上,但是这样引入的css优先级并没有第一种的高</p>
<p>第三种 现在前端都在追求模块化的思想,上述的两种方法,在组件数量非常大的时候,样式污染问题就太痛苦了,所以在进行react开发的时候,普遍都是用,组件独享css样式的方法</p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>CSS Modules</strong></span></p>
<p>CSS Modules 的做法就是通过配置将.css文件进行编译,编译后在每个用到css的组件中的css类名都是独一无二的,从而实现CSS的局部作用域。具体原理可以看看阮一峰老师的博客&nbsp; &nbsp; &nbsp;CSS Modules 用法教程。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">home.module.css{</span>
head:{ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你定义的类名 被import的时候会变成对象的属性</span>
<span style="color: rgba(0, 0, 0, 1)">   color:red;
}
}


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">home.js:</span>
import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import head from</span>'./home.module.css' <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将home.css改成,react使用的webpack 会自动识别module.css文件后缀 然后将其编译</span>
<span style="color: rgba(0, 0, 0, 1)">class Home extends React.Component {
    constructor(props) {
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
            date: </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime()
      }
    }

    componentDidMount() {
      </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> f(){
            console.log(</span>"ffff"<span style="color: rgba(0, 0, 0, 1)">);
      }
      console.log(</span><span style="color: rgba(0, 0, 255, 1)">false</span>||<span style="color: rgba(0, 0, 0, 1)">f())
    }

    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
            </span>&lt;div&gt;
                &lt;div&gt;
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在jsx中使用</span>
                  &lt;span className={head.head}&gt;主页&lt;/span&gt;
                &lt;/div&gt;
                &lt;div&gt;&lt;span&gt;{<span style="color: rgba(0, 0, 255, 1)">this</span>.date}&lt;/span&gt;&lt;/div&gt;
            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">      )
    }
}

export {Home}</span></pre>
</div>
<p>还用其他几种引入css的方法,但其目的和第三种都差不多,都是为了防止样式污染,在此就不一一叙述了,感兴趣的同学可以自己百度</p><br><br>
来源:https://www.cnblogs.com/wrhbk/p/11326202.html
頁: [1]
查看完整版本: react react使用css