reactjs - 如何一次将道具传递给所有样式化的组件css属性?
问题描述
而不是像这样反复挑选道具......
const BubbleContainer = styled.View`
background-color: ${({ theme }) => (theme.debug ? 'white' : 'blue')};
border-width: ${({ theme }) => (theme.debug ? '1px' : '0px')};
color: ${({ theme }) => (theme.debug ? 'red' : 'black')};
`;
我想这样做一次(伪代码)......
const BubbleContainer = styled.View`
${({ theme }) =>
background-color: {theme.debug ? 'white' : 'blue'};
border-width: {theme.debug ? '1px' : '0px'};
color: {theme.debug ? 'red' : 'black'};
}
`;
解决方案
你很亲密。您只需要使用反引号字符串进行插值和多行支持。
const BubbleContainer = styled.View`
${({ theme }) => `
background-color: ${theme.debug ? 'white' : 'blue'};
border-width: ${theme.debug ? '1px' : '0px'};
color: ${theme.debug ? 'red' : 'black'};
`}
`;
有很多选择,这可能更清晰一点:
const BubbleContainer = styled.View`
background-color: blue;
border-width: 0px;
color: black;
${({ theme }) => theme.debug && `
background-color: white;
border-width: 1px;
color: red;
`}
`;
推荐阅读
- java - 无法导入 ZKStringSerializer$
- pine-script - 策略交易视图平均价格 - strategy.position_avg_price
- javascript - promise.then 机制如何工作?
- c++ - 为什么此代码适用于 c++17,但不适用于 c++20 (MSVC)?
- google-chrome-extension - chrome.devtools.network.onRequestFinished - console.log 未执行
- javascript - JupyterLab 导出到 HTML 并隐藏代码按钮安全问题?
- python - Python“”问题
- shell - 查找包含字符串的行并使用 shell 脚本将该值回显到新行
- amazon-web-services - 在 AWS 定价计算器中的哪里可以找到 ECS 和 Fargate?
- ios - 如何在 SwiftUI 中更改 MFMailComposeViewController 导航栏的外观?