reactjs - 道具不存在?使用 styled-component 和 typeScript,已经实现了 `interface`
问题描述
这是我的样式组件
import React from 'react';
import styled, {css} from 'styled-components';
const CategoryButton = styled(Button).attrs({
variant: 'outlined',
})`
${({ theme }) => css`
display: flex;
align-items: center;
justify-content: center;
padding: ${theme.spacing(2)}px ${theme.spacing(4)}px;
background-color: ${(props) =>
props.selected ? theme.colors.primary.main : theme.colors.background};
color: ${(props) =>
props.selected ? theme.colors.background : theme.colors.primary.main};
border-color: ${(props) =>
props.selected ? theme.colors.primary.main : '#c8c8c8'};
`}
`;
它返回一个错误说
Property 'selected' does not exist on type 'ThemeProps<DefaultTheme>'
我已尝试实施有关此其他问题的建议,以指定要传递给组件的道具
interface CategoryButton {
selected?: boolean;
}
const CategoryButton = styled(Button).attrs({
variant: 'outlined',
})<CategoryButton>`
${({ theme }) => css`
...
`}
`;
尽管如此,同样的错误仍然出现。我怎样才能让它工作?
解决方案
你必须像这样selected
进入。CategoryButton
它与传递给孩子的道具相同:
<CategoryButton selected={true}/>
推荐阅读
- python - Pyarrow 不适用于 Python 3.8 ModuleNotFoundError AttributeError
- python - Python 会话 SAMESITE=None 未设置
- hyperledger-fabric - 配置双头 CA
- r - 如何在 R Shiny dataTable 中添加新行而不刷新表中现有的选定数据变量
- javascript - 在Angular 6中将两个json合并为一个
- reactjs - 如果 ReactJS 出现错误,我们如何显示和错误符号(如“!”)?
- python - 我创建了一个有多个敌人的游戏太空入侵者
- python - 我得到`pyiboot01_bootstrap` pyinstaller错误
- c - 为什么文件大小的测量值与缓冲后包含它的字符串的大小不同?
- java - com.google.gson.internal.LinkedTreeMap 类不能转换为类