[英]Pass prop to nested element in styled component
I would like to pass a prop to a nested style for a paragraph element, but it looks like I can't put the prop directly on the element. 我想将prop传递给段落元素的嵌套样式,但是看起来我无法将prop直接放在元素上。 How do I achieve this?
我该如何实现? I only want to apply the uppercase transformation to only one of the p elements...
我只想将大写转换仅应用于p元素之一...
const MailingListWrapper = styled.div` display: flex; & > p { text-transform: ${props => props.uppercase || 'none'}; } ` function JoinUs() { return ( <MailingListWrapper> <p uppercase="uppercase">Join our mailing list!</p> <p>Never miss an update</p> </MailingListWrapper> ) }
The props
you have access to in the template string used when creating the MailingListWrapper
component are the props given to MailingListWrapper
when you use it. 在创建
MailingListWrapper
组件时使用的模板字符串中可以访问的props
是使用时提供给MailingListWrapper
的道具。
You could instead give the p
tag a uppercase
className
and just apply the styles to p
tags with the uppercase
class. 您可以给
p
标签一个uppercase
className
然后将样式应用于带有uppercase
类的p
标签。
const MailingListWrapper = styled.div`
display: flex;
& > p.uppercase {
text-transform: uppercase;
}
`;
function JoinUs() {
return (
<MailingListWrapper>
<p className="uppercase">Join our mailing list!</p>
<p>Never miss an update</p>
</MailingListWrapper>
);
}
Putting the final result here for posterity based on Tholle's answer... 根据Tholle的回答,将最终结果放在此处以供后代参考。
const MailingListWrapper = styled.div` display: flex; & > p { color: gold; &.uppercase { text-transform: uppercase; } } `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.