全国建设管理信息网站,云浮网站建设,设计网页机构,wordpress快捷登录如果想在React中想要像Vue一样把css和js写到一个文件中#xff0c;可以使用CSS-in-JS。
使用CSS-in-JS
下载
npm i styled-components使用
就像写scss一样#xff0c;不过需要声明元素的类型 基本语法及展示如下
import styled from styled-componentsexpor…如果想在React中想要像Vue一样把css和js写到一个文件中可以使用CSS-in-JS。
使用CSS-in-JS
下载
npm i styled-components使用
就像写scss一样不过需要声明元素的类型 基本语法及展示如下
import styled from styled-componentsexport default () {const Father styled.divwidth: 200px;height: 200px;background: pink;span {font-size: 20px;}: hover {background: skyblue;}const Son styled.spancolor: #f8e;return (FatherSon我是Son/Son/Father/)
}也可以通过styled()选择要继承的样式并且可以拿到状态。
import { useState } from react
import styled from styled-components
export default () {const Father styled.divwidth: 200px;height: 200px;background: pink;span {font-size: 20px;}: hover {background: skyblue;}const Son styled.spancolor: #f8e;const Footer styled(Father)display: ${({ isShow }) (isShow ? block : none)};padding: 20px;border: 1px solid #333;border-radius: 30px;const [show, setShow] useState(false)return (FatherSon我是Son/Son/Fatherbutton onClick{() setShow(!show)}点我控制Footer的显示/隐藏/buttonFooter isShow{show}我是Footer/Footer/)
}