安装依赖
npm install styled-components -S
一、定义全局样式
1. styled-components v3 版本
- 创建 style.js
import { injectGlobal } from 'styled-components';
injectGlobal`
body {
margin: 0;
padding: 0;
background:#ccc;
}
`;
- 在 index.js 中引入
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './style.js';
ReactDOM.render(<App />, document.getElementById('root'));
2. styled-components v4 版本以上
- 创建 style.js
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;
}
/* 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;
}
`;
- 在 index.js 中引入并使用
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { GlobalStyle } from './style'
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<GlobalStyle />
<App />
</React.StrictMode>,
);
二、定义组件内样式
1. 创建 style.js 样式文件
新建 src\pages\test\styled.js
import styled from 'styled-components'
export const Wrapper = styled.div`
display: flex;
flex-wrap: wrap;
width: 300px;
height: 300px;
`
export const Item = styled.div`
height: 100px;
width: 100px;
background: red;
text-align:center;
line-height:100px;
&:nth-child(2n-1){
background: yellow;
}
`
2. 在组件中引入并使用 styled.js
新建 src\pages\test\index.js
import React, { Component} from 'react'
import { Wrapper,Item } from './styled'
class Test extends Component{
state = {}
render(){
return (
<Wrapper>
<Item>1</Item>
<Item>2</Item>
<Item>3</Item>
<Item>4</Item>
<Item>5</Item>
<Item>6</Item>
<Item>7</Item>
<Item>8</Item>
<Item>9</Item>
</Wrapper>
)
}
}
export default Test;
三、styled-components 常见用法
1. 传参
在组件标签上绑定参数,通过箭头函数获取并操作参数
import React, { Component} from 'react'
import styled from 'styled-components'
const Wrapper = styled.div`
display: flex;
flex-wrap: wrap;
width: ${props => props.wrapperWidth};
height: ${({wrapperHeight}) => wrapperHeight};
`
const Item = styled.div`
height: 100px;
width: 100px;
background: red;
text-align:center;
line-height:100px;
&:nth-child(2n-1){
background: yellow;
}
`
class Test extends Component{
state = {}
render(){
return (
<Wrapper wrapperWidth="300px" wrapperHeight="300px">
<Item>1</Item>
<Item>2</Item>
<Item>3</Item>
<Item>4</Item>
<Item>5</Item>
<Item>6</Item>
<Item>7</Item>
<Item>8</Item>
<Item>9</Item>
</Wrapper>
)
}
}
export default Test;
2. 继承
通过 styled(父组件) 来继承父组件属性
import React, { Component} from 'react'
import styled from 'styled-components'
const Wrapper = styled.div`
display: flex;
flex-wrap: wrap;
width: 300px;
height: 300px;
`
const CommitItem = styled.div`
height: 100px;
width: 100px;
background: red;
text-align:center;
line-height:100px;
`
const Item = styled(CommitItem)`
&:nth-child(2n-1){
background: yellow;
}
`
class Test extends Component{
state = {}
render(){
return (
<Wrapper>
<Item>1</Item>
<Item>2</Item>
<Item>3</Item>
<Item>4</Item>
<Item>5</Item>
<Item>6</Item>
<Item>7</Item>
<Item>8</Item>
<Item>9</Item>
</Wrapper>
)
}
}
export default Test;
3. 操作 styled 组件属性
可以在组件标签上定义属性,也可以通过 attrs 定义属性
import React, { Component} from 'react'
import styled from 'styled-components'
const PasswordInput = styled.input.attrs(({type,placeholder})=>({
type: type? type : 'text',
placeholder: placeholder || '请输入',
}))`
width: 100px;
background: yellow;
`
class Test extends Component{
state = {
password: '123456'
}
render(){
return (
<PasswordInput defaultValue={this.state.password} type="password" placeholder="请输入密码" ></PasswordInput>
)
}
}
export default Test;
网友评论