1.styled components官网网址
以组件的形式来写样式。
1.1安装
yarn add styled-components
1.2
写法依托于ES6和webpack。
2.Getting Started万物皆组件
把样式定义在组件中
import styled from 'styled-components'
const Title = styled.h1` //h1是标签名,就是<h1></h1>
font-size: 1.5em;
text-align: center;
color: palevioletred;
`
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`
export {
Title,
Wrapper
}
在正式文件中引入把样式定义成组件的文件。
import {Wrapper,Title} from ’......‘
class App extends Component{
render(){
<Wrapper>
<Title>hello<Title>
</Wrapper>
}
}
3.Adapting based on props适配属性
给标签传递属性,在样式组件中去接这个属性。
import styled from 'styled-components'
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: ${props => props.color}; //在组件内部写一个箭头函数来接收参数。通过props获得父组件传过来的参数
background: ${props => props.primary ? "palevioletred" : "white"};
`
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`
export {
Title,
Wrapper
}
在正式文件中引入把样式定义成组件的文件。
import {Wrapper,Title} from ’......‘
class App extends Component{
render(){
<Wrapper>
<Title color=‘red’ primary>hello<Title> //写一个属性,往样式组件中传参。
</Wrapper>
}
}
4.Extending Styles扩展样式
用途:1.继承样式,在原有的样式基础上做一些更新。
2.定义好一组样式,想把这组样式应用到另外一组样式上去。
import styled from 'styled-components'
const Button = styled.button`
color: 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;
`
export {
Button,
TomatoButton
}
在正式文件中引入把样式定义成组件的文件。
import {Button,TomatoButton} from ’......‘
class App extends Component{
render(){
<div>
<Button as="a">按钮</Button> //as的用处是我们可以定义好一个样式组件,用as换成另外一个标签,如这个就换成了a标签。
<TomatoButton>按钮</TomatoButton>
</div>
}
}
5.Styling any components样式化很多组件
import React from 'react'
import styled from 'styled-components'
const Link = (props) => ( //普通组件,不是样式组件
<div>
<a className={props.className}> //必须写,需要用它接收一下增强的样式。
{props.children} //children可以拿到插槽里的内容
</a>
<a>
{props.children}
</a>
</div>
)
const StyledLink = styled(Link)` //给Link定义样式,给本来没有样式的组件加上样式
color: palevioletred;
font-weight: bold;
`
export {
Link,
StyledLink
}
在正式文件中引入把样式定义成组件的文件。
import {Link,StyledLink} from ’......‘
class App extends Component{
render(){
<div>
<StyledLink>hello</StyledLink>
</div>
}
}
6.Passed props传递属性、默认属性
import styled from 'styled-components'
const Input = styled('input')`
padding: 0.5em;
margin: 0.5em;
color: ${ props => props.inputColor };
background: papayawhip;
border: none;
border-radius: 3px;
`
export {
Input
}
在正式文件中引入把样式定义成组件的文件。
import {Input} from ’......‘
class App extends Component{
constructor(){
super()
this.state ={
value:"aaa"
}
}
render(){
<div>
<Input defaultValue={this.state.value}></Input> //给input框设置一个默认值。自定义一个属性,会继承下去,很重要哦!
<Input inputColor="rebeccapurple"></Input>
</div>
}
}
7.Coming from CSS直接引css
import React from 'react'
import styles from './styles.css'
export default class Counter extends React.Component {
state = { count: 0 }
increment = () => this.setState({ count: this.state.count + 1 })
decrement = () => this.setState({ count: this.state.count - 1 })
render() {
return (
<div className={styles.counter}>
<p className={styles.paragraph}>{this.state.count}</p>
<button className={styles.button} onClick={this.increment}>
+
</button>
<button className={styles.button} onClick={this.decrement}>
-
</button>
</div>
)
}}
8.Attaching additional props链接额外组件在props上
import styled from 'styled-components'
const StyledDiv = styled.div.attrs({ //给标签加自己的属性,但不能是自定义属性。
title: 'aaa',
id: 'bbb',
'data-src': (props) => props.hello //可以进行欲添加。
})`
font-size: 100px;
> span {
font-size: 50px;
}
& > span {
font-size: 25px;
}
`
export {
StyledDiv
}
在正式文件中引入把样式定义成组件的文件。
import {StyledDiv} from ’......‘
class App extends Component{
render(){
return(
<StyleDiv hello="hi'>
hello
<span>hahah</span>
</StyleDiv>
)
}
- Android SDK API (2.2,2.3,3.0)中文版文档
转的一篇.觉得很有用. Android SDK API (2.2,2.3,3.0)中文版文档 地址:http://android.laoguo.org固定连接:http://www.laoguo.or ...
- 深度学习框架: Keras官方中文版文档正式发布
今年 1 月 12 日,Keras 作者 François Chollet 在推特上表示因为中文读者的广泛关注,他已经在 GitHub 上展开了一个 Keras 中文文档项目.而昨日,Françoi ...
- Aspose.Cells API 中文版文档 下载
链接: https://pan.baidu.com/s/19foJyWgPYvA7eIqEHJ_IdA 密码: yxun
- DBImport v3.44 中文版发布:数据库数据互导及文档生成工具(IT人员必备)
前言: 距离上一个版本V3.3版本的文章发布,已经是1年10个月前的事了. 其实版本一直在更新,但也没什么大的功能更新,总体比较稳定,所以也不怎么写文介绍了. 至于工作上的事,之前有半年时间跑去学英语 ...
- 英文VS2010安装中文版MSDN文档方法
英文VS2010安装中文版MSDN文档方法 2010-06-01 11:52 by 李永京, 51409 阅读, 50 评论, 收藏, 编辑 在2010年4月12号发布Visual Studio 20 ...
- [中文版] 可视化 CSS References 文档
本文分享了我将可视化 CSS References 文档翻译成中文版的介绍,翻译工作还在陆续进行中,供学习 CSS 参考. 1. 可视化 CSS References 文档介绍 许多 CSS 的文档都 ...
- TensorFlow 官方文档中文版 --技术文档
1.文档预览 2.文档下载 TensorFlow官方文档中文版-v1.2.pdf 提取码:pt7p
- 【转】(五)unity4.6Ugui中文教程文档-------概要-UGUI Interaction Components
原创至上,移步请戳:(五)unity4.6Ugui中文教程文档-------概要-UGUI Interaction Components 4.Interaction Components 本节涵盖了处 ...
- 【转】(四)unity4.6Ugui中文教程文档-------概要-UGUI Visual Components
原创至上,移步请戳:(四)unity4.6Ugui中文教程文档-------概要-UGUI Visual Components 3.Visual Components 有新的组件和游戏对象已添加到uG ...
随机推荐
- [Leetcode]669 Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
- shell高级特性-4
1.shell模块 shell脚本中可以加载配置文件或调用已经完成函数或脚本. a.加载配置文件 新建配置文件config,插入以下代码: reboot_mode=True 在同一路径下,新建文件mo ...
- 使用mpvue开发小程序教程(二)
在上篇文章中,我们介绍了使用mpvue开发小程序所需要的一些开发环境的搭建,并创建了第一个mpvue小程序代码骨架并将其运行起来.在本文中,我们来研究熟悉一下mpvue项目的主要目录和文件结构. 在V ...
- 精读《Serverless 给前端带来了什么》
1. 引言 Serverless 是一种 "无服务器架构",让用户无需关心程序运行环境.资源及数量,只要将精力 Focus 到业务逻辑上的技术. 现在公司已经实现 DevOps 化 ...
- glusterfs+heketi为k8s提供共享存储
背景 近来在研究k8s,学习到pv.pvc .storageclass的时候,自己捣腾的时候使用nfs手工提供pv的方式,看到官方文档大量文档都是使用storageclass来定义一个后端存储服务, ...
- 【面试】我是如何面试别人List相关知识的,深度有点长文
- [四] java8 函数式编程 收集器浅析 收集器Collector常用方法 运行原理 内部实现
Collector常见用法 常用形式为: .collect(Collectors.toList()) collect()是Stream的方法 Collectors 是收集器Collect ...
- Java提高班(五)深入理解BIO、NIO、AIO
导读:本文你将获取到:同/异步 + 阻/非阻塞的性能区别:BIO.NIO.AIO 的区别:理解和实现 NIO 操作 Socket 时的多路复用:同时掌握 IO 最底层最核心的操作技巧. BIO.NIO ...
- Spring Boot(六)集成 MyBatis 操作 MySQL 8
一.简介 1.1 MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集. ...
- 痞子衡嵌入式:一表全搜罗常见移动通信标准(1-5G, GSM/GPRS/CDMA/LTE/NR...)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是移动通信标准. 移动无线网络已经成为我们生活.学习.娱乐不可缺少的必备品,而移动无线通信技术本身也在不断地更新换代.从1986年诞生第一 ...