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 ...
随机推荐
- docker删除镜像和删除容器
删除容器:docker rm ID 删除镜像:docker rmi ID
- [一]class 文件浅析 .class文件格式详解 字段方法属性常量池字段 class文件属性表 数据类型 数据结构
前言概述 本文旨在讲解class文件的整体结构信息,阅读本文后应该可以完整的了解class文件的格式以及各个部分的逻辑组成含义 class文件包含了java虚拟机指令集 和 符号表 以及若 ...
- OI回忆录?
序 一直觉得,回忆录这种东西是签了清北约进了集训队的大爷们站在金字塔的顶端带着一丝轻描淡写的忧愁说一句"退役了啊~",对于我这种省选就退役的人好像没什么资格写这种东西.可还是敲起了 ...
- JDK动态代理浅析
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-06-29/17.html 作者:夜月归途 出处:http://www.guitu ...
- IO通信模型(三)多路复用IO
多路复用IO 从非阻塞同步IO的介绍中可以发现,为每一个接入创建一个线程在请求很多的情况下不那么适用了,因为这会渐渐耗尽服务器的资源,人们也都意识到了这个 问题,因此终于有人发明了IO多路复用.最大的 ...
- 安装VS2010时出现进入的图标没有与需要部分升级VS10Sp1-KB983509的解决方案
大家好,今天又想到需要写一下博客了,毕竟感觉应该在新人入公司的时候可能需要将你电脑上的开发环境进行修改. 下面讲的主要是将VS2012卸载后,重新安装VS2010,. 我遇到了这种情况:在我将VS20 ...
- Mysql中的WITH ROLLUP用法
1.WITH ROLLUP:在group分组字段的基础上再进行统计数据. 例子:首先在name字段上进行分组,然后在分组的基础上进行某些字段统计,表结构如下: CREATE TABLE `test` ...
- Xhprof分析php性能
https://windows.php.net/downloads/pecl/releases/xhprof/0.10.6/ 下载Xhprof版本 配置一个本地访问url,指向index.php,能访 ...
- Java高并发--原子性可见性有序性
Java高并发--原子性可见性有序性 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 原子性:指一个操作不可中断,一个线程一旦开始,直到执行完成都不会被其他线程干扰.换 ...
- 微信小程序--页面的生命周期和参数传递
页面跳转: 1.函数 点击事件设置bindtap然后在本页面 js 设置函数 redirectTo:关闭当前页,跳转到指定页: ( unload) navigateTo:保留当前页,跳转到指定页: ...