reactjs - 尝试访问样式化组件中的自定义道具
问题描述
嗨,我正在尝试访问样式组件中的自定义道具。我知道这是一个非常基本的事情,但我无法弄清楚。我认为这与我访问主题的方式有关。
它不会引发任何错误,但 margin-bottom 值只是没有显示在打印的 css 中。
你能给我指个方向吗?谢谢!
import StyledWrapper from './productCardStyles';
<StyledWrapper spaceBelow={spaceBelow}>
hello world
</StyledWrapper>
//productCardStyles.js
export default styled('div')(
({ theme }) => `
background: red;
margin-bottom: ${props => (props.spaceBelow ? '25px' : '0')};
`);
非常感谢您的宝贵时间。
解决方案
我假设您在高阶组件中使用 ThemeProvider。您只需使用 props.theme 即可访问主题...
更新您的 productCardStyles.js
import styled from 'styled-components'
const wrapper = styled.div`
background: red;
margin-bottom: ${props => (props.spaceBelow ? '25px' : '0')};
color: ${props => props.theme.primaryColor} // to access theme
`
导出默认包装器
推荐阅读
- ionic-framework - 使用离子电容器打开设备设置
- autohotkey - 控制点击在浏览器的后台不起作用
- python - 当另一个客户端连接到服务器时客户端断开连接
- mongodb - 如何使用 mongoDB 中的 bulkupdate 来更新 elasticsearch 中的数据?
- python - 从 JSON url 获取日期字段作为 pandas DataFrame
- bazel - Alpine 容器内的 Bazel 问题
- javascript - 它如何改变触摸事件布尔值?
- python-asyncio - pynng:如何在 REP0 套接字上设置并继续使用多个上下文
- bash - 采购 bash 脚本时避免命令行参数传播
- python - 设置语言级别参数或编译器指令后未找到 Cython .pxd