【发布时间】:2020-02-25 15:19:47
【问题描述】:
import styled from 'styled-components';
<SubTitle length={this.state.name}>{this.state.name}</SubTitle>
我将随机字符串发送到this.state.name,
并且取决于字符串长度,我喜欢设置宽度大小。
const SubTitle = styled.h1`
font-size: 1.2rem;
margin: 20px;
font-weight: bolder;
background-color: rgba(250, 250, 250, 0.5);
width: ${async props => {
let len;
if (props.length) {
len = await props.length.length;
}
return `${len}rem`;
}};
我试过了,但它不起作用。
【问题讨论】:
-
您确定要使用
length作为道具名称吗?它很容易与字符串和数组等内置类型的length属性混淆。 -
你可以使用`width: max-content`