react中使用css的7中方式(应该是最全的)

4次阅读

共计 3648 个字符,预计需要花费 10 分钟才能阅读完成。

第一种: 在组件中直接使用 style
不需要组件从外部引入 css 文件,直接在组件中书写。
import React, {Component} from “react”;

const div1 = {
width: “300px”,
margin: “30px auto”,
backgroundColor: “#44014C”, // 驼峰法
minHeight: “200px”,
boxSizing: “border-box”
};

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div style={div1}>123</div>
<div style=”background-color:red;”>
);
}
}

export default Test;
注意事项:

在正常的 css 中,比如 background-color,box-sizing 等属性,在 style 对象 div1 中的属性中,必须转换成驼峰法,backgroundColor,boxSizing。而没有连字符的属性,如 margin,width 等,则在 style 对象中不变。
在正常的 css 中,css 的值不需要用双引好 (“”),如

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
而在 react 中使用 style 对象的方式时。值必须用双引号包裹起来。
这种方式的 react 样式,只作用于当前组件。
第二种: 在组件中引入 [name].css 文件
需要在当前组件开头使用 import 引入 css 文件。
import React, {Component} from “react”;
import TestChidren from “./TestChidren”;
import “@/assets/css/index.scss”;

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div>
<div className=”link-name”>123</div>
<TestChidren> 测试子组件的样式 </TestChidren>
</div>

);
}
}

export default Test;
这种方式引入的 css 样式,会作用于当前组件及其所有后代组件。
第三种: 3、在组件中引入 [name].scss 文件
引入 react 内部已经支持了后缀为 scss 的文件,所以只需要安装 node-sass 即可,因为有个 node-sass,scss 文件才能在 node 环境上编译成 css 文件。
>yarn add node-sass
然后编写 scss 文件
//index.scss
.App{
background-color: #282c34;
.header{
min-height: 100vh;
color: white;
}
}
关于如何详细的使用 sass,请查看 sass 官网
这种方式引入的 css 样式,同样会作用于当前组件及其所有后代组件。
第四种: 在组件中引入 [name].module.css 文件
将 css 文件作为一个模块引入,这个模块中的所有 css,只作用于当前组件。不会影响当前组件的后代组件。
import React, {Component} from “react”;
import TestChild from “./TestChild”;
import moduleCss from “./test.module.css”;

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}

export default Test;
这种方式可以看做是前面第一种在组件中使用 style 的升级版。完全将 css 和组件分离开,又不会影响其他组件。
第五种: 在组件中引入 [name].module.scss 文件
类似于第四种,区别是第四种引入 css module,而这种是引入 scss module 而已。
import React, {Component} from “react”;
import TestChild from “./TestChild”;
import moduleCss from “./test.module.scss”;

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}

export default Test;
同样这种方式可以看做是前面第一种在组件中使用 style 的升级版。
第六种: 使用 styled-components
需要先安装
>yarn add styled-components
然后创建一个 js 文件 (注意是 js 文件,不是 css 文件)
//style.js
import styled, {createGlobalStyle} from “styled-components”;

export const SelfLink = styled.div`
height: 50px;
border: 1px solid red;
color: yellow;
`;

export const SelfButton = styled.div`
height: 150px;
width: 150px;
color: ${props => props.color};
background-image: url(${props => props.src});
background-size: 150px 150px;
`;
组件中使用 styled-components 样式
import React, {Component} from “react”;

import {SelfLink, SelfButton} from “./style”;

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div>
<SelfLink title=”People’s Republic of China”>app.js</SelfLink>
<SelfButton color=”palevioletred” style={{color: “pink”}} src={fist}>
SelfButton
</SelfButton>
</div>
);
}
}

export default Test;
这种方式是将整个 css 样式,和 html 节点整体合并成一个组件。引入这个组件 html 和 css 都有了。它的好处在于可以随时通过往组件上传入 属性,来动态的改变样式。对于处理变量、媒体查询、伪类等较方便的。
这种方式的 css 也只对当前组件有效。
具体用法,请查看 styled-components 官网
第七种: 使用 radium
需要先安装
>yarn add radium
然后在 react 组件中直接引入使用
import React, {Component} from “react”;
import Radium from ‘radium’;

let styles = {
base: {
color: ‘#fff’,
‘:hover’: {
background: ‘#0074d9’
}
},
primary: {
background: ‘#0074D9’
},
warning: {
background: ‘#FF4136’
}
};

class Test extends Component {
constructor(props, context) {
super(props);
}

render() {
return (
<div>
<button style={[styles.base, styles.primary]}>
this is a primary button
</button>
</div>
);
}
}

export default Radium(Test);
对于处理变量、媒体查询、伪类等是不方便的。
使用 Radium 可以直接处理变量、媒体查询、伪类等,并且可以直接使用 js 中的数学,连接,正则表达式,条件,函数等。
具体用法请查看 radium 官网
注意: 在 export 之前,必须用 Radium 包裹。

正文完
 0