React Css 四种引入方式

React CSS

内联样式

优点
  • 样式之间不会有冲突
  • 可以动态获取组件中state的值
缺点
  • 要使用驼峰标识
  • 部分样式没有很友好的提示
  • 如果大量去写内敛样式 容易造成代码混乱
  • 伪类和伪元素无法编写
react 复制代码
class HighCom extends PureComponent {
    constructor(props) {
        super(props)
        this.state = {
            color: 'red',// 改动这里看结果
            fontSize: '30px'
        }
    }
    render() {
        const { color, fontSize } = this.state
        return (
            <div style={{ color, fontSize }}>
                内联样式
            </div>
        )
    }
}

单独文件css 组件引入

  • 写一个单独的css引入
  • 类似于传统的网页编写
  • 多人合作会造成类名冲突 且相互影响

css模块化

使用
  • .css/.less/.scss 都需要在前面加上module.css/.less/.scss
  • 在组件中引入import myCss from './index.module.css'
  • 在组件中使用 <div className={myCss.className}> css 模块化</div>
优点
  • 解决了React中css局部作用域的问题
缺点
  • 引用的类名不能使用-链接符号 如.box-title 这在js中是不识别的

  • 所有的className 都需要style.className 来写

  • 不方便动态修改样式 还是需要内联去修改

react 复制代码
import React, { PureComponent } from 'react'
import myCss from './index.module.css'

class HighCom extends PureComponent {
    constructor(props) {
        super(props)
        this.state = {
            color: 'red',// 改动这里看结果
            fontSize: '30px'
        }
    }
    render() {
        // const { color, fontSize } = this.state
        return (
            <div className={myCss.textStyle}> css 模块化</div>
        )
    }
}

export default HighCom
css 复制代码
.textStyle {
    color: royalblue;
}

React使用less

  • 修改webpack配置

    • npm run eject

    • npm install @craco/craco craco-less less 推荐使用

    • 最外层新建craco.config.js内容如下

      js 复制代码
      const CracoLessPlugin = require('craco-less');
      
      module.exports = {
          plugins: [
              {
                  plugin: CracoLessPlugin,
                  options: {
                      lessLoaderOptions: {
                          lessOptions: {
                              modifyVars: { '@primary-color': '#1DA57A' }, // 可以在此修改默认的主题变量
                              javascriptEnabled: true,
                          },
                      },
                  },
              },
          ],
      };
    • 修改package.json

      json 复制代码
        "scripts": {
          "start": "craco start",
          "build": "craco build",
          "test": "craco test",
          "eject": "react-scripts eject"
        },

Css in JS styled-components库

介绍
  • CSS-in-JS通过JavaScript来为CSS赋予一些能力,包括类似于CSS预处理器一样的样式嵌套、函数定义、逻辑复用、动态修改状态等等;
  • 虽然CSS预处理器也具备某些能力,但是获取动态状态依然是一个不好处理的点,
安装
  • npm install styled-components
  • 安装vscode插件 vscode-styled-components
基础使用
js 复制代码
 import styled from "styled-components";
 
 export const Container = styled.div`
 .title{
     font-size: 36px;
     font-weight: 600;
     color: yellow;
 }
 `
react 复制代码
 import React, { PureComponent } from 'react'
 import { Container } from './style.js'
 class HighCom extends PureComponent {
     render() {
         return (
             <Container>
                 <div className='title' > css in js </div>
             </Container>
         )
     }
 }
 export default HighCom
继承
js 复制代码
import styled from "styled-components";
export const Container = styled.div`
    font-size: 36px;
    font-weight: 600;
    color: yellow;`
export const Container2 = styled(Container)`
	width:100px
`
//Container2 会继承Container里的样式
属性传递
  • 组件中
react 复制代码
import React, { PureComponent } from 'react'
import { Container } from './style.js'
class HighCom extends PureComponent {
    render() {
        return (
            <Container fontSize={30}>
                <div className='title' > css in js </div>
            </Container>
        )
    }
}
export default HighCom
  • css in js
js 复制代码
import styled from "styled-components";

export const Container = styled.div`
.title{
    font-size: ${props=>props.fontSize}px;
    font-weight: 600;
    color: yellow;
}
  • 设置默认值
js 复制代码
import styled from "styled-components";

export const Container = styled.div.attrs(props=>({titleColor:props.color||"red"}))`
.title{
    font-size: ${props=>props.fontSize}px;
    font-weight: 600;
    color: ${titleColor};
}`
相关推荐
私人珍藏库1 小时前
Google Chrome-便携增强版[解压即用]
前端·chrome
我的青春不太冷2 小时前
【实战篇章】深入探讨:服务器如何响应前端请求及后端如何查看前端提交的数据
运维·服务器·前端·学习
Anlici3 小时前
2025前端高频面试题--CSS篇
前端·css
追光少年33223 小时前
Learning Vue 读书笔记 Chapter 4
前端·javascript·vue.js
软件2053 小时前
【Vite + Vue + Ts 项目三个 tsconfig 文件】
前端·javascript·vue.js
老大白菜3 小时前
在 Ubuntu 中使用 FastAPI 创建一个简单的 Web 应用程序
前端·ubuntu·fastapi
渔阳节度使3 小时前
React
前端·react.js·前端框架
LCG元5 小时前
Vue.js组件开发-如何实现异步组件
前端·javascript·vue.js
Lorcian5 小时前
web前端12--表单和表格
前端·css·笔记·html5·visual studio code
问道飞鱼5 小时前
【前端知识】常用CSS样式举例
前端·css