React中styled-components的使用(样式组件库)

news/2024/7/19 14:09:44 标签: 样式组件库, js, css, html, javascript

一、官网地址

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、引入

javascript">import styled from "styled-components";

3、使用

javascript">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 方法把全局样式包裹在其中

javascript">import { createGlobalStyle } from "styled-components";
export const GlobalStyle = createGlobalStyle`
html" title=css>css">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;
}
javascript">//将 <GlobalStyle /> 组件放在 render() 中最外层元素下面

import React, { Component ,Fragment} from 'react';
import {GlobalStyle} from "./reset";
class App extends Component {
  render() {
    return (
      <Fragment>
        <GlobalStyle/>
      </Fragment>
    );
  }
}

export default App;

五、传参

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

javascript">import {Header} from "style/index.html" title=js>js"

render(){
  return (
        <Header bgColor="red"/>  
    )  
}

style/index.html" title=js>js

javascript">import styled from "styled-components";
export const Header = styled.div`
  width:100px;
  height:200px;
  props.bgColor}
`  

六、继承

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

javascript">const button = styled.button`
  border:0;
  width:100px;
  height:40px;
  text-align:center;
  color:#000;      
`

export const StyledButton = styled(button)`
  color:#fff;  
`

七、修改组件内部标签

在调用组件的时候我们可以通过as来修改组件 as=“元素名称”

javascript">render(){
  return (
    <Header as="p"/>
  )  
}    

Header组件内部渲染的时候就是用的p标签

八、定义组件属性

javascript">export const Input = styled.input.attrs({
    value:(props)=>props.value,
    name:"input"
})`
  border:0;
  width:100px;
  height:100px;
`

九、背景图片引入

javascript">import logo from "./imgs/logo.png";

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

十、塑造组件

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

javascript">import React from "react";
import styled from "styled-components";

const Link = ({className,children})=>(
        <a className={className}>
             {children}
         </a>   
)    
export StyleLink = styled(Link)`
  color:red  
`

javascript_193">十一、动画```javascript

javascript">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;
`

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

&代表父级

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

         }
    }  

http://www.niftyadmin.cn/n/1527230.html

相关文章

Redux中间件(redux-thunk、redux-promise、redux-saga)

文章目录1、redux中间件简介1.1、什么是redux中间件1.2、使用redux中间件2、中间件的运行机制2.1、createStore源码分析2.2、applyMiddleware源码分析3、常见的redux中间件3.1、logger日志中间件3.2、redux异步管理中间件3.2.1、redux-thunk3.2.2、redux-promise3.2.3、redux-s…

Drupal总结

Drupal如何导入中文包。 在英文版安装好了以后&#xff0c;点击顶部的菜单“Modules”&#xff0c;进入模块管理页面(admin/modules)&#xff0c;找到“Locale”模块&#xff0c;将其开启。 接着点击“Configuration” > “Languages”&#xff0c;进入语言管理界面(admin/…

数学原理(The Principles of Mathmatics)

文章目录参考感悟第二版&#xff08;Bertrand Russell&#xff09;第二版介绍参考 https://archive.org/details/principlesofmath005807mbp 感悟 感觉作者就是一个话痨。但是确实是把问题、理论讲的非常的清楚。其实教材就是应该这样写&#xff0c;自问自答的方式&#xff…

Network: ARP vs arping

文章目录 参考命令arping-A-c-D-I-q命令 arp查询删除Gratuitous ARP (GARP)样例系统参数arp_ignore 整数proxy ARP标志含义系统参数anycast_delay (since Linux 2.2)app_solicit (since Linux 2.2)base_reachable_time (since Linux 2.2)

gcc: c/c++编译错误总结

文章目录 c++ 11两个井号的含义:- 运行时错误13编译错误14No such file or directoryerror: for loop initial declarations are only allowed in C99 modeerror: anonymous variadic macros were introduced in C99 [-Werror=variadic-macros]error: expected initializer be…

errno总结

文章目录 参考errno简介多线程下的问题errno的位置及error代码的定义相关汇编代码errno描述打印实例Linux系统下查看errno含意的工具详解512 ERESTARTSYSEINTR例子1例子2#define ENOBUFS 105 /* No buffer space available */122 EDQUOT111 ECONNREFUSEDENOTCONN 107ECONNRESET…

ip 命令示例

## 添加 vxlan 接口 ip -6 link add vxlan100 type vxlan id 100 dstport 4789 local 2001:db8:1::1 group ff05::100 dev eth0 ttl 5 ## -d 选项 显示详细信息: [root@qrms6-host01 mzhan017]# ip -d link show vxlan100 308: vxlan100: <BROADCAST,MULTICAST> mtu…

React-redux高阶组件数据共享

文章目录案例效果&#xff1a;实现过程&#xff1a;案例效果&#xff1a;目录结构&#xff1a;1、src/index.js2、src/App.js3、src/redux3.1、src/redux/incrementReducer3.1.1、src/redux/incrementReducer/index.js3.1.2、src/redux/personReducer/index.js3.3、src/redux/s…