styled-components的基本使用
一、官网地址
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:(props)=>props.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 }
} `
styled-components的基本使用的更多相关文章
- styled components草根中文版文档
1.styled components官网网址 https://www.styled-components.com/docs 以组件的形式来写样式. 1.1安装 yarn add styled-c ...
- 6周学习计划,攻克JavaScript难关(React/Redux/ES6 etc.)
作者:余博伦链接:https://zhuanlan.zhihu.com/p/23412169来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 和大家一样,最近我也看了Jo ...
- 大前端的自动化工厂(2)—— SB Family
原文链接:https://bbs.huaweicloud.com/blogs/53c0c3509b7a11e89fc57ca23e93a89f 我坦白我是标题党,SB只是SCSS-Bourbon的简写 ...
- 使用styled-components实现CSS in JS
前面的话 使用jsx语法可以实现HTML in JS,使用svgr可以实现svg in JS,使用styled-components可以实现CSS in JS.这样,使用react开发,就变成了使用J ...
- Next.js v4.1.4 文档中文翻译【转载】
最近想稍稍看下 React的 SSR框架 Next.js,因为不想看二手资料, 所以自己跑到 Github上看,Next.js的文档是英文的,看倒是大概也能看得懂, 但有些地方不太确定,而且英文看着毕 ...
- CSS Overrides: Friend or Foe?
转自:http://www.callumhart.com/blog/css-overrides-friend-or-foe Anyone familiar with CSS will know how ...
- 9 CSS in JS Libraries You Should Know in 2018
转自:https://blog.bitsrc.io/9-css-in-js-libraries-you-should-know-in-2018-25afb4025b9b 实际上 wix 的 styl ...
- 推荐 9 个样式化组件的 React UI 库
简评:喜欢 CSS in JS 吗?本文将介绍一些使用样式组件所构建的 React UI 库,相信你会很感兴趣的. 在 React 社区,对 UI 组件进行样式化的讨论逐步从 CSS 模块到内联 CS ...
- The Road to learn React书籍学习笔记(第一章)
react灵活的生态圈 Small Application Boilerplate: create-react-app Utility: JavaScript ES6 and beyond Styli ...
- Angular Vue React 框架中的 CSS
框架中的 CSS Angular Vue React 三大框架 Angular Vue 内置样式集成 React 一些业界实践 Angular Angular . js (1.x):没有样式集成能力 ...
随机推荐
- JSR107缓存规范
Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvider定义了创建. ...
- WPF界面假死
首先要检查那些滥用 Timer.Dispacher Timer 或者滥用什么“线程+死循环+阻塞”轮询的代码. 这种是编程大忌,有些人不会设计事件驱动程序,而是滥用轮询. 若是:触发事件后的假死,搜W ...
- 关于window.localtion的用法几点总结
参考链接: http://blog.csdn.net/cui_angel/article/details/7957274(1)window.location.pathname设置或获取对象指定的文件名 ...
- 使用EasyPOI导出excel示例
package com.mtoliv.sps.controller; import java.io.IOException; import java.io.OutputStream; import j ...
- sql 2012 用户sa登陆不上
1.通过Windows身份验证进入服务器 2.点击“安全性”--“登录名”--“sa” 3.右击sa,点击“属性” 4.取消“强制实施密码策略(F)”勾选 5.对密码重新输入 6.点击确定,重新用SQ ...
- md 常用语法
序言: 起因: 因为现在的前端基本上都用上了前端构建工具,那就难免要写一些readme等等的说明性文件,但是这样的文件一般都是.md的文件,编写的语法自然跟其他格式的文件有所区别,置于为什么要用这种格 ...
- scrapy 入门爬取新闻
为文本分类实验爬取数据集,要求一百万,分类>10类. 参考链接:http://litianyi.cc/technology/2015/12/01/text-classification-1/ 文 ...
- Linux c codeblock的使用(二):在工程中编译多个文件
(一)前言 我们刚开始学习linux c的时候,一般都是在一个c文件里面写完所有程序,然后用gcc编译这个c文件就好了,十分简单. 但是你有没有想过,如果我们希望将不同模块的代码放到不同的c文件,然后 ...
- SQL Server2008R2循环语句
单循环语句 declare @i nvarchar(36) declare @LOCNUM nvarchar(36),@OBJECTTYPE nvarchar(36),@LOCDESC nvarcha ...
- 基本promise
function myPromise(fn) { var value = null, callbacks = []; this.then = function (onFulfilled) { call ...