reactjs - 样式化组件 - 使用样式化组件作为另一个组件的基础
问题描述
我认为这对于样式化的组件是可能的
使用第一个样式组件Block
作为另一个组件的基础,例如
export const BlockOne = styled.Block
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
import styled from 'styled-components'
export const Block = styled.div`
width: 100px;
height: 100px;
background: red;
`
export const BlockOne = styled.Block`
background: yellow;
`
const App = () => {
return (
<div>
<Block/>
<BlockOne/>
</div>
);
}
render(<App />, document.getElementById('root'));
有没有办法做到这一点
解决方案
是的,像这样
export const BlockOne = styled(Block)`
background: yellow;
`
styled-component 仅具有基本组件(例如 div、span 等标签)作为属性。对于其他任何事情,您将其作为道具传递。
如果您将自定义组件传递给它,请确保它接受 className 并将其传递给 div 或其他东西:
const MyComponent = ({className}) => {
return <div className={className}></div> // styled-component will use this classname to apply the style
}
export const MyStyledComponent = styled(MyComponent)`
background: yellow;
`
否则它不会有任何影响。
推荐阅读
- ios - Swift - 更改 Tabbar 的高度后刷新视图控制器
- swift - 在 Appkit SwiftUI 中更改按钮颜色会破坏格式
- python-3.x - Pyinstaller Errno 22 无效参数:
- r - 如何使用 dplyr 到 group_by() 折叠子系列
- javascript - 在“回调队列”的上下文中,“空堆栈”到底是什么意思?
- logstash - Logstash 过滤器中的 Grok 和 mutate 对输出没有影响
- c - 如何验证 scanf 数字输入
- java - 内存数据库spring-boot中的h2
- spring-boot - 为什么 Kotlin main vararg args: String 不能直接传递给 SpringBoot 的 runApplication?
- node.js - mongoose 导入后无法检测到 _id