React 中使用CSS的几种方式

分手后的思念是犯贱 2024-04-17 05:32 121阅读 0赞

第一种: 在组件中直接使用style

不需要组件从外部引入css文件,直接在组件中书写。

  1. import React, { Component } from "react";
  2. const div1 = {
  3. width: "300px",
  4. margin: "30px auto",
  5. backgroundColor: "#44014C", //驼峰法
  6. minHeight: "200px",
  7. boxSizing: "border-box"
  8. };
  9. class Test extends Component {
  10. constructor(props, context) {
  11. super(props);
  12. }
  13. render() {
  14. return (
  15. <div>
  16. <div style={ div1}>123</div>
  17. <div style="background-color:red;">
  18. </div>
  19. );
  20. }
  21. }
  22. export default Test;

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

  1. .App-header {
  2. background-color: #282c34;
  3. min-height: 100vh;
  4. display: flex;
  5. flex-direction: column;
  6. align-items: center;
  7. justify-content: center;
  8. font-size: calc(10px + 2vmin);
  9. color: white;
  10. }

而在react中使用style对象的方式时。值必须用双引号包裹起来。
这种方式的react样式,只作用于当前组件。

第二种: 在组件中引入[name].css文件

需要在当前组件开头使用import引入css文件。

  1. import React, { Component } from "react";
  2. import TestChidren from "./TestChidren";
  3. import "@/assets/css/index.css";
  4. class Test extends Component {
  5. constructor(props, context) {
  6. super(props);
  7. }
  8. render() {
  9. return (
  10. <div>
  11. <div className="link-name">123</div>
  12. <TestChidren>测试子组件的样式</TestChidren>
  13. </div>
  14. );
  15. }
  16. }
  17. export default Test;

这种方式引入的css样式,会作用于当前组件及其所有后代组件。

第三种: 在组件中引入[name].scss文件

引入react内部已经支持了后缀为scss的文件,所以只需要安装node-sass即可,因为有个node-sass,scss文件才能在node环境上编译成css文件。

yarn add node-sass

然后编写scss文件

  1. //index.scss .App{
  2. background-color: #282c34; .header{
  3. min-height: 100vh;
  4. color: white;
  5. }
  6. }

关于如何详细的使用sass,请查看sass官网
这种方式引入的css样式,同样会作用于当前组件及其所有后代组件。

第四种: 在组件中引入[name].module.css文件

将css文件作为一个模块引入,这个模块中的所有css,只作用于当前组件。不会影响当前组件的后代组件。

  1. import React, { Component } from "react";
  2. import TestChild from "./TestChild";
  3. import moduleCss from "./test.module.css";
  4. class Test extends Component {
  5. constructor(props, context) {
  6. super(props);
  7. }
  8. render() {
  9. return (
  10. <div>
  11. <div className={ moduleCss.linkName}>321321</div>
  12. <TestChild></TestChild>
  13. </div>
  14. );
  15. }
  16. }
  17. export default Test;

这种方式可以看做是前面第一种在组件中使用style的升级版。完全将css和组件分离开,又不会影响其他组件。

第五种: 在组件中引入 [name].module.scss文件

类似于第四种,区别是第四种引入css module,而这种是引入 scss module而已。

  1. import React, { Component } from "react";
  2. import TestChild from "./TestChild";
  3. import moduleCss from "./test.module.scss";
  4. class Test extends Component {
  5. constructor(props, context) {
  6. super(props);
  7. }
  8. render() {
  9. return (
  10. <div>
  11. <div className={ moduleCss.linkName}>321321</div>
  12. <TestChild></TestChild>
  13. </div>
  14. );
  15. }
  16. }
  17. export default Test;

同样这种方式可以看做是前面第一种在组件中使用style的升级版。

第六种: 使用styled-components

需要先安装

yarn add styled-components

然后创建一个js文件(注意是js文件,不是css文件)

  1. //style.js
  2. import styled, { createGlobalStyle } from "styled-components";
  3. export const SelfLink = styled.div` height: 50px; border: 1px solid red; color: yellow; `;
  4. 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样式

  1. import React, { Component } from "react";
  2. import { SelfLink, SelfButton } from "./style";
  3. class Test extends Component {
  4. constructor(props, context) {
  5. super(props);
  6. }
  7. render() {
  8. return (
  9. <div>
  10. <SelfLink title="People's Republic of China">app.js</SelfLink>
  11. <SelfButton color="palevioletred" style={ { color: "pink" }} src={ fist}>
  12. SelfButton
  13. </SelfButton>
  14. </div>
  15. );
  16. }
  17. }
  18. export default Test;

这种方式是将整个css样式,和html节点整体合并成一个组件。引入这个组件html和css都有了。它的好处在于可以随时通过往组件上传入 属性,来动态的改变样式。对于处理变量、媒体查询、伪类等较方便的。
这种方式的css也只对当前组件有效。具体用法,请查看styled-components官网。
第七种: 使用radium
需要先安装

yarn add radium

然后在react组件中直接引入使用

  1. import React, { Component } from "react";
  2. import Radium from 'radium';
  3. let styles = {
  4. base: {
  5. color: '#fff',
  6. ':hover': {
  7. background: '#0074d9'
  8. }
  9. },
  10. primary: {
  11. background: '#0074D9'
  12. },
  13. warning: {
  14. background: '#FF4136'
  15. }
  16. };
  17. class Test extends Component {
  18. constructor(props, context) {
  19. super(props);
  20. }
  21. render() {
  22. return (
  23. <div>
  24. <button style={ [ styles.base, styles.primary ]}>
  25. this is a primary button
  26. </button>
  27. </div>
  28. );
  29. }
  30. }
  31. export default Radium(Test);

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

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读