[英]Typescript React.ComponentProps is not getting the props
在props
中找不到boxProp
。
库版本:
├─ react@16.13.1
└─ styled-components@5.2.1
这不起作用,因为您实际上并没有将任何道具传递给您的Box
组件,第一行所做的只是注释您的Box
组件可以使用哪些道具 - 因此 React 在使用ComponentProps
方法时没有要提取哪些道具的上下文.
当您自己注释道具类型时,一个简单的解决方法是执行以下操作:
type BoxProps = { boxProp: boolean }
const Box = styled('div')<BoxProps>
const MyBox: React.FC<BoxProps> = ({ boxProp }) => {
return (
<Box boxProp={boxProp} />
)
}
让我知道这是否有帮助!
这是我最后一次尝试解决这个问题:
type OwnProps = { boxProps?: boolean };
const Box = styled.div<OwnProps>``;
type CustomBoxProps = StyledComponentProps<'div', DefaultTheme, OwnProps, never>;
export const MyBox: React.FC<CustomBoxProps> = (props) => {
return (<Box {...props} />);
};
不幸的是,必须使用StyledComponentProps
。 React.ComponentProps
不适用于 styled-components 接口。 肯定是他们那边出了问题。
请让我知道这是否可以解决您的问题,它应该
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.