react 样式的写法之一 ---》styled-components的基本使用
【react】---styled-components的基本使用---【WangQi】
一、官网地址
https://www.styled-components.com/
二、styled-components
1、styled-components 样式化组件,主要作用是它可以编写实际的CSS代码来设计组件样式,也不需要组件和样式之间的映射,即创建后就是一个正常的React 组件,并且可以附加样式给当前组件。 优化react组件
2、在一个组件内会将结构、样式和逻辑写在一起,虽然这违背了关注点分离的原则,但是这有利于组件间的隔离。为了顺应组件化的潮流
3、使用styled-components不需要再使用className属性来控制样式,而是将样式写成更具语义化的组件的形式
4、使用style-components会随机生成一个class名称,这样不会污染到全局变量,当然因为随机生成,维护会增加难度
三、基本使用
1、安装
cnpm i styled-components -S || yarn add styled-components
2、引入
import styled from "styled-components";
3、使用

export const Header = styled.div`
width:100%;
height:1rem;
background:red
` import {Header} from "./style/index";
class App extends Component{
render(){
return (
<Header/>
)
}
}

四、全局默认样式引入
引入新的API createGlobalStyle ,在下面创建一个 GlobalStyle 变量,用 createGlobalStyle 方法把全局样式包裹在其中import { createGlobalStyle } from "styled-components";export const GlobalStyle = createGlobalStyle`

html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img { margin:0; padding:0; }
fieldset, c{ border:none; }
img{display: block;}
address, caption, cite, code, dfn, th, var { font-style:normal; font-weight:normal; }
ul, ol ,li{ list-style:none; }
body { color:#333; font:12px BASE "SimSun","宋体","Arial Narrow",HELVETICA; background:#fff;}
a { color:#666; text-decoration:none; }
*{box-sizing:border-box}
body,html,#root{
height: 100%;
overflow: hidden;
}
`
//将 <GlobalStyle /> 组件放在 render() 中最外层元素下面

五、传参
如果我们需要动态改变元素的样式,则可以通过传递参数的方式进行改变

import {Header} from "style/index.js"
render(){
return (
<Header bgColor="red"/>
)
}
style/index.js
import styled from "styled-components";
export const Header = styled.div`
width:100px;
height:200px;
props.bgColor}
`

六、继承
如果我们需要继承样式的时候我们可以通过 styled(继承的组件名称)``

const button = styled.button`
border:0;
width:100px;
height:40px;
text-align:center;
color:#000;
` export const StyledButton = styled(button)`
color:#fff;
`

七、修改组件内部标签
在调用组件的时候我们可以通过as来修改组件 as="元素名称"

render(){
return (
<Header as="p"/>
)
}
Header组件内部渲染的时候就是用的p标签

八、定义组件属性

export const Input = styled.input.attrs({
value:"请输入密码",
name:"input"
})`
border:0;
width:100px;
height:100px;
`

九、背景图片引入

import logo from "./imgs/logo.png"; export const BgLogo = styled.div`
width:100px;
height:200px;
background:url(${logo}) no-repate;
`

十、塑造组件
有一种情况,一些原本就已经是组件,需要给这些组件添加样式,这时需要用到塑造组件

import React from "react";
import styled from "styled-components"; const Link = ({className,children})=>(
<a className={className}>
{children}
</a>
) export StyleLink = styled(Link)`
color:red
`

十一、动画

const move = keyframes`
0%{
transform:rotate(0%);
} 100%{
transform :rotate(100%); }
` export const TransFormDiv = styled.div`
width:100px;
height:100px;
background:red;
animation:${move} 2s; `

十二、当标签过多时需要划分太多组件,我们可以通过以下写法来简化组件的编写
&代表父级

export const StyledUl = styled.ul`
border:1px solid #ccc;
>li{
border-bottom:1px solid #green;
line-height:30px;
padding-left:20px;
&>p{
color:red }
} `

react 样式的写法之一 ---》styled-components的基本使用的更多相关文章
- styled components草根中文版文档
1.styled components官网网址 https://www.styled-components.com/docs 以组件的形式来写样式. 1.1安装 yarn add styled-c ...
- Vue+element-ui 重置组件样式的写法
两种方式实现element-ui组件的样式 方案1:重置的公共组件样式的写法如下 然后在main.js中引入 import '@/assets/css/element.css' 方案2:每个.vu ...
- javascript常用函数封装——运动、cookie、ajax、获取行内样式兼容写法、拖拽
运动.cookie.ajax.获取行内样式兼容写法.拖拽封装大合集. //url,data,type,timeout,success,error function ajax(options){ //- ...
- css三种样式表写法
css三种样式表:1.内嵌样式表: <head> <style type="text/css"> 样式表写法 </style> < ...
- react className 有多个值时的处理 / react 样式使用 百分比(%) 报错
1.react className 有多个值时的处理 <fieldset className={`${styles.formFieldset} ${styles.formItem}`}> ...
- react 样式冲突解决方案 styled-components
前置 在 react 中解决组件样式冲突的方案中,如果您喜欢将 css 与 js 分离,可能更习惯于 CSS-Modules:如果习惯了 Vue.js 那样的单文件组件,可能习惯于使用 styled- ...
- 玩转React样式
很久很久以前,就有人用CSS来时给HTML内容添加样式.CSS可以最大限度的分离样式和内容,选择器也可以很方便的给某些元素添加样式.你根本找不到任何不用CSS的理由. 但是在React这里就是另外一回 ...
- IE样式兼容写法
1.第一种写法 利用<!--[if lt IE 6/7/8/9/10/11]><![endif]-->,给每个html写一个class <!DOCTYPE html> ...
- [React Fundamentals] Using Refs to Access Components
When you are using React components you need to be able to access specific references to individual ...
随机推荐
- 内置time模块和random模块
#time模块#time模块中有三种时间表达方式#时间戳(timestamp):指从1970年1月1号0:0:0开始按秒计算的时间偏移量#元组形式的结构化时间(strut_time):含有9个元素(t ...
- Comet OJ - Contest #5 D 迫真小游戏 (堆+set)
迫真小游戏 已经提交 已经通过 时间限制:2000ms 内存限制:256MB 73.98% 提交人数:196 通过人数:145 题目描述 H君喜欢在阳台晒太阳,闲暇之余他会玩一些塔防小游戏. H君玩的 ...
- 最小可观(Minimal Observability Problem in Conjunctive Boolean Networks)
论文链接 1. 什么是 conjunctive Boolean network (CBN) 仅仅包含and运算. 下面这个式子为恒定更新函数 2. 什么是可观 定义在时刻k是CBN的状态为 X(k) ...
- socket tcp clinet最简单测试程序
// testsocketclient.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <winsock2.h> ...
- du df的用法
1,两者区别 du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在 的,没有被删除的.他计算的大小就是当前他认为存在的所有文件大小的累加和. df, ...
- 在 LaTeX 中同步缩放 TikZ 与其中的 node
PGF/TikZ 是 Till Tantau 开发的基于 TeX 的绘图引擎.因其可以直接在 LaTeX 文稿中通过代码绘制向量图,所以是目前流行的 LaTeX 绘图解决方案之一. 在 tikzpic ...
- WPF导出发布安装包,无法验证发行者解决办法
右击工程项目点发布->完成 此时在工程目录下生成了安装文件setup.exe 同时又在工程下生成了临时证书WpfApp1_TemporaryKey.pfx 如果此时强行安装会弹出如下警告 接下来 ...
- LeetCode--072--编辑距离(python)
给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符删除一个字符替换一个字符示例 1: 输入: ...
- 面试题常考&必考之--js中的对象的浅拷贝和深拷贝(克隆,复制)(下)
这里主要是讲深拷贝: 深拷贝:个人理解就是拷贝所有的层级 1.像对象里再放数组和对象这些叫引用值.开始我们先判断大对象中是否有引用值(数组和小对象), 然后在判断引用值是数组还是对象 2.开始啦: 1 ...
- C++11 lambda表达式小结
目录 简介 结构 return type parameter list capture list 值捕获和引用捕获 变量修改 隐式和显式捕获 捕获列表小结: problems 1.为什么需要使用mut ...