reactjs - Material ui 图标的 React + Typescript 错误

标签 reactjs typescript material-ui

我从 github 帮助页面为图标定义了以下内容:

const tableIcons = {
  Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
  DetailPanel: forwardRef((props, ref) => (
    <ChevronRight {...props} ref={ref} />
  )),
  Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
  NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
  PreviousPage: forwardRef((props, ref) => (
    <ChevronLeft {...props} ref={ref} />
  )),
  ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
  Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
  SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />),
  ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
  ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />),
};

然后我在这里称之为:
<MaterialTable
      icons={tableIcons}

但是我收到这个错误,任何帮助表示赞赏。当页面最初加载时,我可以看到图标,但随后它们消失了,我收到此错误。对不起,大块的代码
No overload matches this call.
  Overload 1 of 2, '(props: { component: ElementType<any>; } & { children?: ReactNode; color?: "inherit" | "disabled" | "action" | "primary" | "secondary" | "error" | undefined; fontSize?: "small" | ... 3 more ... | undefined; htmlColor?: string | undefined; shapeRendering?: string | undefined; titleAccess?: string | undefined; viewBox?: string | undefined; } & CommonProps<...> & Pick<...>): Element', gave the following error.
    Property 'component' is missing in type '{ ref: ((instance: unknown) => void) | MutableRefObject<unknown> | null; children?: ReactNode; }' but required in type '{ component: ElementType<any>; }'.
  Overload 2 of 2, '(props: DefaultComponentProps<SvgIconTypeMap<{}, "svg">>): Element', gave the following error.
    Type '((instance: unknown) => void) | MutableRefObject<unknown> | null' is not assignable to type '((instance: SVGSVGElement | null) => void) | RefObject<SVGSVGElement> | null | undefined'.
      Type 'MutableRefObject<unknown>' is not assignable to type '((instance: SVGSVGElement | null) => void) | RefObject<SVGSVGElement> | null | undefined'.
        Type 'MutableRefObject<unknown>' is not assignable to type 'RefObject<SVGSVGElement>'.
          Types of property 'current' are incompatible.
            Type 'unknown' is not assignable to type 'SVGSVGElement | null'.
              Type 'unknown' is not assignable to type 'SVGSVGElement'.  TS2769

    21 | 
    22 | const tableIcons = {
  > 23 |   Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
       |                                     ^
    24 |   Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
    25 |   Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
    26 |   Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),

最佳答案

我也有这个问题。如果您使用自定义 SVG 图标,请将其声明为 SVGIcon 而不是仅返回 <svg>{...}</svg> .
例如:

import * as React from "react";
import SvgIcon, { SvgIconProps } from "@material-ui/core/SvgIcon";

const CustomIcon: React.FC<SvgIconProps> = (props) => {
    return (
        <SvgIcon {...props}>
            <path {...} />
        </SvgIcon>
    );
};

export default CustomIcon;
这将帮助您在不添加 <SVGSVGElement, {}> 的情况下摆脱错误。前面forwardRef .

关于reactjs - Material ui 图标的 React + Typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61902377/

相关文章:

javascript - 从字符串中找到 '*' 、 '**' 和 '` 并替换为 <strong></strong> 和 <code></cod in javascript, Typescript

javascript - React Material-UI Select 使用对象数组作为源

node.js - 如何计算sequelize js中的关联

javascript - 为什么组件重新渲染后输入元素没有重新获得焦点?

reactjs - onLoadEnd 不会在 react-native Image 中触发

javascript - 如何在 React 中修改具有子级多个级别的父 DOM 上的状态

reactjs - Material-UI 在打开对话框时向 body 标签添加填充

node.js - 服务器和客户端的路由 - React.js 和 Node.js

reactjs - 找不到@types/react-bootstrap-table2-editor

typescript - 无法将字符串值分配给 typescript 枚举(初始化程序类型字符串不可分配给变量类型)