前言
随着web应用越来越复杂,组件化的开发方式逐渐被大多数的公司及开发者所使用。组件化的开发方式是将结构、样式和逻辑放到一起,组合成一个可重用性比较强的组件。styled-component是一个实现在js中写css样式,将样式封装成一个组件的react的第三方工具。
用法
安装:
在react项目根目录执行以下命令:
npm install --save styled-components
或
yarn add styled-components
全局重置样式
在react的入口文件同级目录新建style.js文件,引入 styled-components 并全局重置css,具体实现如下:
import { createGlobalStyle } from 'styled-components';
export const Globalstyle = 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;
}
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;
}
`;
在react入口文件中引用:
import React, {Component, Fragment} from 'react';
import { Provider } from 'react-redux';
import { BrowserRouter, Route} from 'react-router-dom';
import store from './store/index';
import { Globalstyle } from './style'; // 全局样式控制
import Home from './containers/Home/RootContainer/index';
class App extends Component {
render() {
return (
<Fragment>
<Provider store={store}>
<BrowserRouter>
<Fragment>
{/* exact 完全匹配路由 */}
<Route path="/" exact component={Home} />
</Fragment>
</BrowserRouter>
</Provider>
<Globalstyle />
</Fragment>
);
}
}
export default App;
页面中的使用
构建组件,在home.js 同级目录下新建style.js文件,定义ContentWrapper 和 HomeContent组件,用于页面存放内容。在home.js 文件中引入,具体如下:
style.js
import styled from 'styled-components';
export const HomeWrapper = styled.div`
overflow: hidden;
width: 1000px;
margin: 0 auto;
color: red;
`;
export const HomeContent = styled.div`
float: left;
width: 800px;
margin-left: 15px;
color: green;
`;
home.js
import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import {
HomeWrapper, HomeContent
} from './style.js';
const mapDispatch = (dispatch) => {
return {
}
};
const mapState = (state) => {
return {
}
};
class Home extends PureComponent {
render() {
return (
<HomeWrapper>
内容1
<HomeContent>
内容2
</HomeContent>
</HomeWrapper>
)
}
}
export default connect(mapState, mapDispatch)(Home);
组建内子标签样式 使用className
<HomeWrapper>
内容1
<HomeContent>
内容2
<div className='homeContent_div'>
内容3
</div>
</HomeContent>
</HomeWrapper>
style.js
export const HomeContent = styled.div`
float: left;
width: 800px;
margin-left: 15px;
color: green;
.homeContent_div {
color: #000;
}
`;
参数传递
<HomeWrapper>
内容1
<HomeContent>
内容2
<div className='homeContent_div'>
内容3
</div>
<BackgroundDiv className='homeContent_div' imgUrl="https://cn.bing.com/images/search?q=%e5%9f%8e%e5%b8%82%e5%a4%9c%e6%99%af%e6%91%84%e5%bd%b1&FORM=ISTRTH&id=3AD1608F3918A3D5789F65FED97453F3E1BDC647&cat=%E6%91%84%E5%BD%B1&lpversion=">
内容4
</BackgroundDiv>
</HomeContent>
</HomeWrapper>
style.js
import styled from 'styled-components';
export const BackgroundDiv = styled.div`
width: 300px;
height: 100px;
background: url(${ (props)=>props.imgUrl });
background-size: contain;
`;
styled-components 支持所有的css样式及写法,后面用到更深层的用法会及时分享。
期待共同学习!
网友评论