【发布时间】:2022-01-18 17:40:21
【问题描述】:
我有一个组件Box,看起来像这样:
const Box = ({ className, country }) => {
const status = myStatusFunction(country);
return (
<div className={className} status={status}>
{country}
</div>
);
};
我想在我的视图中使用这个组件,但还要根据status 属性为其添加自定义样式,所以我的styled.js 看起来像这样:
import _Box from 'components/Box';
const Box = styled(_Box)`
${({ status }) => {
if (status === 'available') {
return css`
background-color: green;
`;
}
return css`
background-color: red;
`;
}}
`
这里的问题是status 在这里是undefined,所以我的问题是——你能以某种方式访问status 属性吗?
【问题讨论】:
标签: javascript reactjs styled-components