```
#### Styled Components(动态)
```jsx
import styled from 'styled-components'
const Device = styled.div`
background-color: ${props => props.isActive ? '#28a745' : '#0769fb'};
color: ${props => props.isActive ? 'white' : 'black'};
transform: ${props => props.isActive ? 'scale(1.05)' : 'scale(1)'};
transition: all 0.3s;
`
// 直接使用,自动根据 props 变化
```
**优势:** 样式完全由 JavaScript 控制,可以根据任何条件动态生成。
---
### 2. 主题系统(SCSS 需要手动实现)
#### SCSS(手动实现)
```scss
// 需要写多个主题类
.theme-light { background: #fff; color: #000; }
.theme-dark { background: #1a1a1a; color: #fff; }
```
```jsx
```
#### Styled Components(内置主题)
```jsx
import styled, { ThemeProvider } from 'styled-components'
const Container = styled.div`
background-color: ${props => props.theme.bg};
color: ${props => props.theme.text};
`
const theme = {
light: { bg: '#fff', text: '#000' },
dark: { bg: '#1a1a1a', text: '#fff' }
}
// 一键切换主题
内容
```
**优势:** 内置主题系统,切换主题无需修改组件代码。
---
### 3. 组件绑定(SCSS 样式和组件分离)
#### SCSS(分离)
```scss
// device.scss
.device-container { }
.device-title { }
.device-button { }
```
```jsx
// device.jsx
import './device.scss'
标题
```
#### Styled Components(绑定)
```jsx
import styled from 'styled-components'
const DeviceContainer = styled.div`
width: 100%;
background-color: #0769fb;
`
const DeviceTitle = styled.h1`
font-size: 24px;
color: white;
`
const DeviceButton = styled.button`
padding: 12px 24px;
background: white;
border: none;
border-radius: 4px;
`
// 样式和组件完全绑定,不会冲突
function Device() {
return (
标题
按钮
)
}
```
**优势:** 样式和组件绑定,自动作用域隔离,不会产生样式冲突。
---
### 4. 条件样式(SCSS 需要写多个类)
#### SCSS(多个类)
```scss
.button { }
.button-primary { background: blue; }
.button-danger { background: red; }
.button-large { padding: 20px; }
.button-small { padding: 10px; }
```
```jsx