今天准备来给大家分享分享React中styled-components的基础使用,仅仅是我个人的一些理解,不一定全对,有错误还请大佬们指出,496838236这是我qq,有想指点我的大佬随时加我qq好吧,要是想约我一起做保健,那我只能随叫随到了

好了,废话不多说,开工

 今天我们不对react的环境进行搭建,我直接用脚手架搭一个最简单的环境来用,进入主题

 1.使用styled-components

  首先我们要安装styled-components

yarn add styled-components  ||  npm install --save styled-components

2.最基础的使用,(为了方便阅读,以下所有的代码我将在一个文件中演示)

  

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //修改了div的样式
const Title = styled.div`
font-size:1.5rem;
color:red
`
// 修改了button的样式
const Button = styled.button`
border:none;
background-color:blue
` class App extends Component {
render() {
return (
<Fragment>
{
// 开始的内容
/* <div>大红牛</div>
<button>枸杞11</button> */}
<Title>大红牛</Title>
<Button>枸杞</Button>
</Fragment>
)
}
}
export default App;

运行结果:

是不是很爽,其实到这里完全就可以用styled-components来写类似于CSS的代码了,但是这肯定不够啊,所以我们继续往下看

2.塑造组件

  为什么要有塑造塑件呢,因为肯定会有一个场景,我们要对已经定义好的组件进行二次样式的修改,那这个时候我们就需要用塑造组件了

import React, { Component,Fragment} from 'react';
//引入styled-components
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 YellowButton = styled(Button)`
background-color:yellow
` class App extends Component {
render() {
return (
<Fragment>
<Button>红牛</Button>
<YellowButton>枸杞</YellowButton>
</Fragment>
)
}
}
export default App;

运行结果:

3.props传递参数  styled-components可以用props接受参数,从而根据传递的参数确定样式,是不是很强大

 

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //props传递参数(根据参数的值设置样式)
// 有传递值字体会变为红色
// 无传递值会默认取蓝色
const Button = styled.button`
padding: 0.5em;
margin: 0.5em;
color: ${ props => props.inputColor || "blue" };
background: papayawhip;
border: none;
border-radius: 3px;
` class App extends Component {
render() {
return (
<Fragment>
<Button inputColor="red">红牛啊</Button>
</Fragment>
)
}
}
export default App;

运行结果:

除了可以根据参数的值进行样式的设置之外,我们还可以通过参数的有无来设置样式:

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //props传递参数(根据参数的有无设置样式)
// 有参数背景会变为蓝色
// 无传递值背景会默认取黄色
const Button = styled.button`
padding: 0.5em;
margin: 0.5em;
background: ${props=>props.blue?"blue":"yellow"};
border: none;
border-radius: 3px;
` class App extends Component {
render() {
return (
<Fragment>
<Button blue>红牛啊</Button>
</Fragment>
)
}
}
export default App;

运行结果:

.修改样式的同时添加属性,同时也可以通过这种方法引入第三方的样式,只需要设置className属性即可

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //props传递参数(根据参数的有无设置样式)
// 有参数背景会变为蓝色
// 无传递值背景会默认取黄色
const Button = styled.button.attrs({
title:"aaa",
id:'bbb',
'data-role':(props)=>props.hello
})`
padding: 0.5em;
margin: 0.5em;
border: none;
border-radius: 3px;
` class App extends Component {
render() {
return (
<Fragment>
<Button hello="hi">红牛啊</Button>
</Fragment>
)
}
}
export default App;

运行结果:

  通过控制台我们可以看到,属性已经全部被加上去了

5.继承

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //继承 根据实验 如果我没出错 最新的版本应该是不支持extend了
const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border-radius: 3px; ` const YellowButton = Button.extend`
color: yellow;
border-color: yellow; `; class App extends Component {
render() {
return (
<Fragment>
<Button>红牛啊</Button>
<YellowButton>枸杞啊</YellowButton>
</Fragment>
)
}
}
export default App;

6.偷懒的写法,当标签很多时,要是我们虽每个标签都要进行修饰,那岂不是要写好多的组件,但是在有些情况下我们没必要分这木多组件,那我们不妨可以试试以下的写法

  

import React, { Component,Fragment} from 'react';
//引入styled-components
import styled from 'styled-components' //另一种语法
const StyledDiv = styled.div`
font-size: 100px;
> span {
font-size: 50px;
}
& > p {
font-size: 25px;
}
` class App extends Component {
render() {
return (
<Fragment>
<StyledDiv>
<span>红牛</span>
<p>枸杞</p>
</StyledDiv>
</Fragment>
)
}
}
export default App;

运行结果:

好啦,好啦今天就先到这里吧,希望各位大佬能指教指教我,实在不想指教一起约个正规保健也是可以的好吧

  

  

  

  

React中使用styled-components的基础使用的更多相关文章

  1. React中最基础的jsx语法

    import React, { Component } from 'react'; class App extends Component { render() { return ( <div ...

  2. styled components草根中文版文档

    1.styled components官网网址 https://www.styled-components.com/docs   以组件的形式来写样式. 1.1安装 yarn add styled-c ...

  3. 【Web技术】401- 在 React 中使用 Shadow DOM

    本文作者:houfeng 1. Shadow DOM 是什么 Shadow DOM 是什么?我们先来打开 Chrome 的 DevTool,并在 'Settings -> Preferences ...

  4. 聊一聊 React 中的 CSS 样式方案

    和 Angular,Vue 不同,React 并没有如何在 React 中书写样式的官方方案,依靠的是社区众多的方案.社区中提供的方案有很多,例如 CSS Modules,styled-compone ...

  5. React中的合成事件

    React中的合成事件 React自己实现了一套高效的事件注册.存储.分发和重用逻辑,在DOM事件体系基础上做了很大改进,减少了内存消耗,简化了事件逻辑,并最大程度地解决了IE等浏览器的不兼容问题. ...

  6. React中使用CSSTransitionGroup插件实现轮播图

    动画效果,是一个页面上必不可少的功能,学习一个新的东西,当然就要学习,如何用新的东西,用它的方法去实现以前的东西啦.今天呢,我就在这里介绍一个试用react-addons-css-transition ...

  7. React 深入系列1:React 中的元素、组件、实例和节点

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加深对React的理解,以及在项目中 ...

  8. React 中阻止事件冒泡的问题

    在正式开始前,先来看看 JS 中事件的触发与事件处理器的执行. JS 中事件的监听与处理 事件捕获与冒泡 DOM 事件会先后经历 捕获 与 冒泡 两个阶段.捕获即事件沿着 DOM 树由上往下传递,到达 ...

  9. [Web 前端] mobx教程(三)-在React中使用Mobx

    copy from : https://blog.csdn.net/smk108/article/details/85053903 Mobx提供了一个mobx-react包帮助开发者方便地在React ...

随机推荐

  1. Thinking in Java from Chapter 10

    From Thinking in Java 4th Edition 内部类 public class Parcel1 { class Contents { private int i = 11; pu ...

  2. [CocoaPods]使用Trunk进行设置

    CocoaPods Trunk CocoaPods Trunk是一种身份验证和CocoaPods API服务.要将新的或更新的库发布到CocoaPods以进行公开发布,您需要在Trunk中注册并在当前 ...

  3. 微信开发之获取openid及推送模板消息

    有很多的朋友再问我怎么获取code,openid之类的问题,在这里我就给大家分享一下. 在做微信支付是需要获取openid的,推送模板消息也是需要openid包括其他一些功能分享等也都是需要的,ope ...

  4. 实现文本在标签内平均分布的css样式

    这里有一个容器,添加了一段文字,想让它们平均分布达到标签flex布局的效果,而不是靠左.靠右或者居中显示. 添加样式: text-align: justify; text-align-last: ju ...

  5. Docker 部署Django项目

    使用docker部署django项目也很简单,挺不错,分享下 环境 默认你已安装好docker环境 django项目大概结构 (p3s) [root@opsweb]# tree opsweb opsw ...

  6. [Leetcode]59.螺旋矩阵Ⅱ

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

  7. 比较List和ArrayList的性能及ArrayList和LinkedList优缺点

    List和ArrayList的性能比较 在使用ArrayList这样的非泛型集合的过程中,要进行装箱和拆箱操作,会有比较大的性能损失,而使用泛型集合就没有这样的问题.List是泛型,而ArrayLis ...

  8. conda环境里安装pydot

    一.conda环境里安装pydot, 输入以下命令即可: conda install -c anaconda pydot 二.如果运行tensorflow,提示缺什么包,都可以在这里下载. ----- ...

  9. oracle查锁及解锁命令

    --查询行锁语句 select sql_text from v$sql a,v$session b where a.sql_id=b.sql_id and b.event='enq: TX - row ...

  10. php几种常见排序算法

    <?php //从时间上来看,快速排序和归并排序在时间上比较有优势,//但是也比不上sort排序,归并排序比较占用内存! $arr = [4,6,1,2,3,89,56,34,56,23,65] ...