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})
`

styled-components:解决react的css无法作为组件私有样式的问题的更多相关文章

  1. CSS Modules 解决 react 项目 css 样式互相影响的问题

    1. CSS Modules引入目的 写过CSS的人,应该都对一大长串选择器选中一个元素不陌生吧,这种方式,其实定义的就是全局样式,我们时常会因为选择器权重问题,没有把我们想要的样式加上去. 另外,每 ...

  2. Github css加载失败,样式混乱解决办法

    github被墙的解决办法 Github css加载失败,样式混乱解决办法   打开cmd,输入  nslookup github.com 8.8.8.8  ,下面就会显示出github的服务器地址列 ...

  3. styled components草根中文版文档

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

  4. 解决React首屏加载白屏的问题

    众所周知,在项目中如果在资源加载请求还未完成的时候,由于阻塞机制,会出现首页白屏的问题,产生很差的用户体验.本文以react为例,提供一个解决方法. 解决原理:使用 onreadystatechang ...

  5. [React] Render Basic SVG Components in React

    React loves svg just as much as it loves html. In this lesson we cover how simple it is to make SVG ...

  6. 解决React Native unable to load script from assets index.android.bundle on windows

    React Native运行的时候,经常碰到React Native unable to load script from assets index.android.bundle on windows ...

  7. 解决React Native使用Fetch API请求网络报Network request failed

    问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...

  8. springboot 解决配置js/css/img缓存问题

    # 解决配置js/css/img缓存问题 spring.resources.chain.strategy.content.enabled=true spring.resources.chain.str ...

  9. react 调用 function 的写法 及 解决 react onClick 方法自动执行

    1.react 调用方法的写法 (1)方式一 onClick={this.getFetchData.bind(this,item.id)} (2)方式二 getFetchData(e){ this.s ...

随机推荐

  1. #在windows上使用ngix重定向目录访问远程服务器文件详细实例

    为了在开发环境保持于生产环境相同的访问远程服务器文件资源的目录配置,需要在开发环境(windows)在远程文件服务器使用nignx重定向文件目录,因为网上的资料大都是copy的,解释比较笼统,也没有具 ...

  2. Ubuntu&Mac下使用alias简化日常操作

    alias 在Linux系统中用来给指令起别名,用来简化很长的指令. 用法很简单: alias : 查看当前所有别名 alias tmstart = "$CATALINA_HOME/bin/ ...

  3. 使用Navicat连接阿里云服务器中的Mysql数据库

    1.首先将阿里云服务器中的安全组添加上Mysql的端口3306,如下图所示: 步骤就是进入到阿里云的官网,点击右上角控制台,在左边选择云服务器ECS--->实例 点击图中的管理按钮,然后选择本实 ...

  4. 《Windows内核安全与驱动开发》 3.2 内存与链表

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 3.2 内存与链表 1. 尝试生成一个链表头并将其初始化. 2. 尝试向内存 ...

  5. 【限时免费】从入门到实战,5节课玩转Kafka!赢音箱、书籍好礼!

    欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字"加群",加入华为云线上技术讨论群:输入关键字"最新活动",获取华 ...

  6. Python-TCP客户端程序开发

    TCP客户端,需要与服务端建立连接,连接建立成功后才可以进行数据的传输. # 1.导入模块 import socket if __name__ == '__main__': # 2.创建套接字对象 t ...

  7. “Unknown class XXViewController in Interface Builder file.”问题处理

    在静态库中写了一个XXViewController类,然后在主工程的xib中,将xib的类指定为XXViewController,程序运行时,报了如下错误: “Unknown class XXView ...

  8. 数据库Oracle的含义

    数据库的含义: 所谓的数据库其实就是数据的集合.用户可以对集合中的数据进行新增.查询.更新. 删除等操作.数据库是以一定方式储存在一起.能与多个用户共享.具有尽可能小的冗余度. 与应用程序彼此独立的数 ...

  9. POJ1704 Georgia and Bob(Nim博弈变形)

    Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14312   Accepted: 4840 ...

  10. 怎么样才算是精通 Python?

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:weixin_45587093 你们所说的什么要怎样数据分析.爬虫.We ...