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})
`
styled-components:解决react的css无法作为组件私有样式的问题的更多相关文章
- CSS Modules 解决 react 项目 css 样式互相影响的问题
1. CSS Modules引入目的 写过CSS的人,应该都对一大长串选择器选中一个元素不陌生吧,这种方式,其实定义的就是全局样式,我们时常会因为选择器权重问题,没有把我们想要的样式加上去. 另外,每 ...
- Github css加载失败,样式混乱解决办法
github被墙的解决办法 Github css加载失败,样式混乱解决办法 打开cmd,输入 nslookup github.com 8.8.8.8 ,下面就会显示出github的服务器地址列 ...
- styled components草根中文版文档
1.styled components官网网址 https://www.styled-components.com/docs 以组件的形式来写样式. 1.1安装 yarn add styled-c ...
- 解决React首屏加载白屏的问题
众所周知,在项目中如果在资源加载请求还未完成的时候,由于阻塞机制,会出现首页白屏的问题,产生很差的用户体验.本文以react为例,提供一个解决方法. 解决原理:使用 onreadystatechang ...
- [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 ...
- 解决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 ...
- 解决React Native使用Fetch API请求网络报Network request failed
问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...
- springboot 解决配置js/css/img缓存问题
# 解决配置js/css/img缓存问题 spring.resources.chain.strategy.content.enabled=true spring.resources.chain.str ...
- react 调用 function 的写法 及 解决 react onClick 方法自动执行
1.react 调用方法的写法 (1)方式一 onClick={this.getFetchData.bind(this,item.id)} (2)方式二 getFetchData(e){ this.s ...
随机推荐
- hadoop全分布式的搭建
修改主机名:vim /etc/sysconfig/network 1 修改 hadoop-env.sh 2 修改core-site.xml /hadoop/tmpdir: 产生 namenode中fs ...
- 2019年12月4日Linux开发手记
OK,经过昨天对V4L2工作流程的学习,现在已经大体了解了V4L2的工作原理,现在开始对V4L2的API的学习,目标:1.打开摄像头 2.储存图像 3.关闭摄像头,API网址:Linux Media ...
- P2669 金币
题目描述 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币:之后四天(第七.八.九.十天),每 ...
- PringData JPA一对多多对一多对多关联
一.一对多.多对一 1.Country实体类 2.City实体类 3.CountryDao层 4.CityDao层 5.Controller package com.zn.controller; im ...
- Python高级数据结构-Collections模块
在Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了之中,认识了python基本的数据类型和数据结构,现在认识一个高级的:Collections 这个模块对上面的数据结构做了封装,增加 ...
- [ch05-02] 用神经网络解决多变量线性回归问题
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力 5.2 神经网络解法 与单特征值的线性回归问题类似,多变量 ...
- ES6——async函数
目录 1.async 函数是 Generator 函数的语法糖. 2.async函数对 Generator 函数的改进,体现在以下四点. 3.基本用法 一个获取股票报价的函数 指定多少毫秒后输出一个值 ...
- [TimLinux] Python 装饰器
1. 装饰器 一种语法格式,用于替换另外一个编码风格,一种语法糖,通过语法结构明确标识出这样一种语法. 自动在被装饰对象尾部执行代码(不使用装饰器语法时,需要明确写明的代码) 被装饰对象可以为函数.类 ...
- linux 安装jmeter
一 下载jdk sudo apt install oracle-java8-installer 二 网站下载 jmeter 三 对jmeter文件夹 赋权 我都是777 chmod -R 777 ap ...
- python学习笔记-生成随机数
更多大数据分析.建模等内容请关注公众号<bigdatamodeling> 在实现算法时经常会用到随机数,有时会忘记各种随机数的生成方法,这里对Python中的随机数生成方法进行汇总,以供以 ...