【react】---styled-components的基本使用---【巷子】

1、官網地址
  https://www.styled-components.com/

2、styled-componentshtml

  一、styled-components 樣式化組件,主要做用是它能夠編寫實際的CSS代碼來設計組件樣式,也不須要組件和樣式之間的映射,即建立後就是一個正常的React 組件,而且能夠附加樣式給當前組件。 優化react組件react

  二、在一個組件內會將結構、樣式和邏輯寫在一塊兒,雖然這違背了關注點分離的原則,可是這有利於組件間的隔離。爲了順應組件化的潮流npm

  三、使用styled-components不須要再使用className屬性來控制樣式,而是將樣式寫成更具語義化的組件的形式組件化

  四、使用style-components會隨機生成一個class名稱,這樣不會污染到全局變量,固然由於隨機生成,維護會增長難度優化

 

3、基本使用動畫

  一、安裝url

  cnpm i styled-components -S    ||    yarn add styled-componentsspa

  二、引入設計

  import styled from "styled-components";code

  三、使用

export const Header = styled.div`
  width:100%;
  height:1rem;
  background:red      
`

import {Header} from "./style/index";
class App extends Component{
  render(){
    return (
      <Header/>
    )
  }
}

 

4、全局默認樣式引入

引入新的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 React, { Component ,Fragment} from 'react';
import {GlobalStyle} from "./reset";
class App extends Component {
  render() {
    return (
      <Fragment>
        <GlobalStyle/>
      </Fragment>
    );
  }
}

export default App;
 

 

5、傳參

 若是咱們須要動態改變元素的樣式,則能夠經過傳遞參數的方式進行改變

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;
  background-color:${props=>props.bgColor}
`

 

 6、繼承

  若是咱們須要繼承樣式的時候咱們能夠經過 styled(繼承的組件名稱)``

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

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

 

 7、修改組件內部標籤

  在調用組件的時候咱們能夠經過as來修改組件  as="元素名稱"

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

Header組件內部渲染的時候就是用的p標籤

 

 8、定義組件屬性

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

`

9、背景圖片引入

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

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

10、塑造組件

  有一種狀況,一些本來就已是組件,須要給這些組件添加樣式,這時須要用到塑造組件

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

const Link = ({className,children})=>(
        <a className={className}>
             {children}
         </a>   
)    


export StyleLink = styled(Link)`
  color:red  
`

11、動畫

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;

`

 

12、當標籤過多時須要劃分太多組件,咱們能夠經過如下寫法來簡化組件的編寫

  &表明父級

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

         }
    }  

`    
相關文章
相關標籤/搜索