发布日期:2023-05-26 浏览次数:2

说明
1、CSS中in JS,意思就是使用js语言写css,完全不需要些单独的css文件,所有的css代码全部放在组件内部,以实现css的模块化。
2、CSS in JS其实是一种编写思想。
目前已经有超过40多种方案的实现,最出名的是 styled-components。
实例
import React from "react";import styled from "styled-components"; // 创建一个带样式的 h1 标签const Title = styled.h1` font-size: 1.5em; text-align: center; color: palevioletred;`; // 创建一个带样式的 section 标签const Wrapper = styled.section` padding: 4em; background: papayawhip;`; // 通过属性动态定义样式const Button = styled.button` background: ${props => (props.primary ? "palevioletred" : "white")}; color: ${props => (props.primary ? "white" : "palevioletred")}; font-size: 1em; margin: 1em; padding: 0.25em 1em; border: 2px solid palevioletred; border-radius: 3px;`; // 样式复用const TomatoButton = styled(Button)` color: tomato; border-color: tomato;`; Hello World, this is my first styled component! Primary;