styled-components:解決react的css無法作為組件私有樣式的問題


react中的css在一個文件中導入,是全局的,對其他組件標簽都會有影響。

使用styled-components第三方模塊來解決,並且styled-components還可以將標簽和樣式寫到一起,作為一個有樣式的組件,這樣樣式就是這個組件的私有樣式,不會給其他組件造成影響,也很方便。

下包:

npm i styled-components

公共類的寫法如下:把.css文件改為.js文件,代碼如下:

import {createGlobalStyle} from 'styled-components';
export const GlobalStyled = createGlobalStyle`
html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, 
    figure, figcaption, footer, header, hgroup, 
    menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
    }
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure, 
    footer, header, hgroup, menu, nav, section {
        display: block;
    }
    body {
        line-height: 1;
    }
    ol, ul {
        list-style: none;
    }
    blockquote, q {
        quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }`

再將公共的css組件放入到App根組件中,這樣style.js中的css樣式全局通用了

import React from 'react';
import {GlobalStyled} from './style.js';
import Header from './common/header'
function App() {
  return (
    <div className="App">
      <GlobalStyled/>
      <Header/>
    </div>
  );
}

export default App;

組件的私有類,寫法如下:注意下導入寫法

import styled from 'styled-components'
import logoPic from '../../statics/jianshulogo.png'
export const HeaderWrapper = styled.div`
    position: relative;
    height: 56px;
    border-bottom: 1px solid #f0f0f0;
`

export const Logo = styled.a`
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    width: 100px;
    height: 56px;
    background: url(${logoPic});
    background-size: contain;
`

有樣式的組件,哪里需要放哪里就行了,styled-components構造出來的組件,一般react標簽有的屬性,組件都有, 不過react標簽中ref屬性,在styled組件中是用innerRef。

import React,{Component} from "react"
import {HeaderWrapper,Logo} from "./style"

class Header extends Component {
    render(){
        return (
            <HeaderWrapper>
                <Logo href="/"></Logo>
            </HeaderWrapper>
 )
    }
}

export default Header

也可以把屬性寫到組件內,如下:

export const Logo = styled.a.attr({
    href: '/'
})`
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    width: 100px;
    height: 56px;
    background: url(${logoPic});
    background-size: contain;
`

 

 再注意一個問題,之前學習配置webpack的時候,我們知道.css,.less,sacs等樣式文件,webpack配置后是可以將其解析的,像background:url(),根據路徑webpack會打包圖片也是沒有問題的,但是這里用styled-components,是不會解析background:url(),不會解析路徑,獲取圖片的,而只是當成一個路徑字符串,返回到網頁中去

例如如下圖的代碼:

 

 網頁中返回的是一個路徑字符串,webpack並沒有幫我們把圖片打包出來, 如下圖,當然localhost:3000是沒有上一級目錄的,找不到圖片的

 

 所以這時候我們需要手動導入圖片

import styled from 'styled-components' import logoPic from '../../statics/jianshulogo.png'
export const HeaderWrapper = styled.div`
    position: relative;
    height: 56px;
    border-bottom: 1px solid #f0f0f0;
`

export const Logo = styled.a`
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    width: 100px;
    height: 56px;
    background: red url(${logoPic})
`

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM