javascript - 在 `createGlobalStyle` 中使用样式化组件的规则
问题描述
样式化组件的新手,想知道是否有人可能对如何在调用中使用样式化组件的规则有一些建议createGlobalStyle
?
下面的示例正在运行,但我觉得这不是一个很好的解决方案,因为componentStyle.rules
官方 api 文档中没有。
// A styled component
import Modal from '../Modal'
import styled, { createGlobalStyle } from 'styled-components'
const StyledComponent = styled(Modal)`
background-color: pink;
`
createGlobalStyle`
// this div is mounted outside of the React root
.modal-from-external-library {
${StyledComponent.componentStyle.rules}
}
`
解决方案
不确定我尝试做的事情是否可行,但我最终通过使用css
样式组件的功能从 Modal 导出 css 来解决问题。
// Modal.js
const styles = css`
// styles here
`
export default styled.div`
${styles}
`
// ... later
const GlobalStyles = createGlobalStyle`${styles}`
render() { return (<GlobalStyles {...props} />) }
推荐阅读
- sql - SQL Server 视图定期超时
- java - Spark-Scala-Intellij java.lang.IllegalStateException:安装 macOS Big Sur 更新后
- cordova - 为 Android SDK Platform 30 构建 ionic cordova 应用程序
- python - Pyspark - 计算两个数据帧之间的日期
- android - React Native firebase实时数据库不起作用
- c++ - 查找数组范围内最大和最小元素之间的差异
- c# - 将原始字符串解析为对象列表
- mask - GIMP Script-Fu:使用蒙版批量编辑图像
- reactjs - 用于同一项目中的 express 和 react 项目的 Eslint
- c# - 如何将外观相似的 C# 方法归结为通用方法