简体   繁体   English

属性“组件”不存在 reactjs + materialUI + typescript?

[英]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 元素。

Docs 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ReactJS和Typescript定义组件实例属性? 属性“ var”在类型“ App”上不存在 - How to define component instance attributes with ReactJS and Typescript? `Property 'var' does not exist on type 'App'` 组件未呈现(ReactJS/MaterialUI) - Component Not Rendering (ReactJS/MaterialUI) 打字稿:属性不存在 - Typescript: Property does not exist 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript 使用本机Web组件时出现打字稿错误“类型&#39;JSX.IntrinsicElements&#39;上不存在属性” - Typescript error “Property does not exist on type 'JSX.IntrinsicElements'” when using native web component Typescript Vue 组件中的 Pinia 商店给出错误“&#39;CreateComponentPublicInstance&#39; 类型上不存在属性 &#39;testStore&#39;....” - Pinia store in Typescript Vue component gives error "Property 'testStore' does not exist on type 'CreateComponentPublicInstance'...." React Class 组件属性中的 TypeScript 错误在类型“Readonly&lt;{}&gt;”上不存在,不确定如何为 state 设置类型 - TypeScript errors in React Class Component property does not exist on type 'Readonly<{}>', not sure how to set types for state TypeScript-类型“节点”上不存在属性“ id” - TypeScript - Property 'id' does not exist on type 'Node'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM