安裝依賴(lài)
npm install styled-components -S
一、定義全局樣式
1. styled-components v3 版本
- 創(chuàng)建 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 版本以上
- 創(chuàng)建 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>,
);
二、定義組件內(nèi)樣式
1. 創(chuàng)建 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;
}
`
// 定義非 div 標(biāo)簽樣式
export const Input = styled.input`
width: 350px;
height: 30px;
line-height: 30px;
padding: 0 10px;
color: red;
`
2. 在組件中引入并使用 styled.js
新建 src\pages\test\index.js
import React, { Component } from "react";
import { Wrapper, Item, Input } 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>
<div>
<label htmlFor="inp">
用戶(hù)名 <Input placeholder="請(qǐng)輸入用戶(hù)名" id="inp" />
</label>
</div>
</>
);
}
}
export default Test;
三、styled-components 常見(jiàn)用法
1. 傳參
在組件標(biāo)簽上綁定參數(shù),通過(guò)箭頭函數(shù)獲取并操作參數(shù)
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. 繼承
通過(guò) styled(父組件) 來(lái)繼承父組件屬性
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 組件屬性
可以在組件標(biāo)簽上定義屬性,也可以通過(guò) attrs 定義屬性
import React, { Component} from 'react'
import styled from 'styled-components'
const PasswordInput = styled.input.attrs(({type,placeholder})=>({
type: type? type : 'text',
placeholder: placeholder || '請(qǐng)輸入',
}))`
width: 100px;
background: yellow;
`
class Test extends Component{
state = {
password: '123456'
}
render(){
return (
<PasswordInput defaultValue={this.state.password} type="password" placeholder="請(qǐng)輸入密碼" ></PasswordInput>
)
}
}
export default Test;