[英]Property 'component' does not exist reactjs + materialUI + typescript?
I am creating custom typography.我正在创建自定义排版。 But while using I am getting below error.
但是在使用时我遇到了以下错误。
I am following below document我正在关注以下文件
https://mui.com/material-ui/api/typography/ https://mui.com/material-ui/api/typography/
here is my code https://codesandbox.io/s/fragrant-mountain-eukirg这是我的代码https://codesandbox.io/s/fragrant-mountain-eukirg
import * as React from "react";
import Typography, { TypographyProps } from "@mui/material/Typography";
import { styled } from "@mui/material/styles";
const LabelXs = styled(Typography)<TypographyProps>(({ fontWeight }) => {
return {
fontSize: 15
};
});
export default LabelXs;
I am using like this我这样用
<LabelXs component={"div"}>Div element</LabelXs>
don't why it is showing error?不为什么它显示错误?
[![Property 'component' does not exist on type 'IntrinsicAttributes & SystemProps<Theme> & { align?: "right" | "left" | "inherit" | "center" | "justify" | undefined; children?: ReactNode; ... 6 more ...; variantMapping?: Partial<...> | undefined; } & CommonProps & Omit<...> & MUIStyledCommonProps<...>'.ts(2322)][1]][1]
any suggestion?有什么建议吗?
Component seems not to be present in TypographyProps. TypographyProps 中似乎没有组件。 Therefore, inspired by @SteveGomez, you can do something like:
<TypographyProps & {component: React.ElementType}>
因此,受@SteveGomez 的启发,您可以执行以下操作:
<TypographyProps & {component: React.ElementType}>
import * as React from "react";
import Typography, { TypographyProps } from "@mui/material/Typography";
import { styled } from "@mui/material/styles";
const LabelXs = styled(Typography)<TypographyProps & {component: React.ElementType}>(({ fontWeight }) => {
return {
fontSize: 15
};
});
export default LabelXs;
EDIT: You could also use {component: keyof JSX.IntrinsicElements}
.编辑:您也可以使用
{component: keyof JSX.IntrinsicElements}
。
To be able to use the component prop, the type of the props should be used with type arguments. Just use React.ElementType as mentioned in the docs.为了能够使用组件道具,道具的类型应与类型 arguments 一起使用。只需使用文档中提到的 React.ElementType。
const LabelXs = styled(Typography)<
TypographyProps & { component: React.ElementType }
>(({ fontWeight }) => {
return {
fontSize: 15
};
});
Now component
accepts any React Custom Component and HTML element.现在
component
接受任何 React 自定义组件和 HTML 元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.