reactjs - 通过 props 控制组件的样式属性
问题描述
具有以下代码:
export type BreadcrumbItemProps = {
isCurrent?: boolean;
};
const isCurrent = (props: { isCurrent?: boolean }) => props.isCurrent ? 'normal' : 'bold';
export const Item = styled.span<BreadcrumbItemProps>`
display: inline;
padding: 10px;
cursor: default;
font-weight: ${ props => isCurrent(isCurrent)};
`;
根据isCurrent
我想控制字体的道具,无论是正常还是粗体,尝试这样做时出现以下错误:
const isCurrent: (props: { isCurrent?: boolean | undefined; }) => "normal" | "粗体" 类型 '(props: { isCurrent?: boolean | undefined; }) => "normal" | "bold"' 与类型 '{ isCurrent?: boolean | 没有共同的属性 不明确的; }'.ts(2559)
解决方案
您不需要函数,您应该能够像这样内联逻辑:
export type BreadcrumbItemProps = {
isCurrent?: boolean;
};
export const Item = styled.span<BreadcrumbItemProps>`
display: inline;
padding: 10px;
cursor: default;
font-weight: ${ props.isCurrent ? 'normal' : 'bold'};
`;
推荐阅读
- javascript - 在js中组合对象并保留键
- ruby-on-rails - 如何在 Heroku 上为 Sidekiq 设置预部署挂钩
- javascript - 在悬停时添加指向图像的链接
- python - 如何在windows中安装scrapy
- android - 如何使用视图模型从另一个片段访问函数
- php - 如何使用 ajax .post() 将数据传递给 php
- latex - 乳胶中的 APA6 样式 - 如何在摘要页面上隐藏标题?
- sql - 每分钟插入的记录数
- swift - 如何使用 UIDocumentPickerViewController 获取文件的完整路径?
- node.js - 为什么邮递员给我“未找到 404”错误?