简体   繁体   English

TypeScript 的 React Arrow 组件的类型是什么?

[英]What is the type of a React Arrow Component for TypeScript?

Component File组件文件

// Component A

import React, { ReactElement } from "react";

export const BacklogItemDashboard = (): ReactElement => (
    <h1>Backlog Item Dashboard</h1>
);

Use Component File使用组件文件

import // component

interface IMenuInfo {
    choice: string;
  **component: ReactElement;**  
    destination: string;
}

export const BacklogItem = (): ReactElement => {
    const menuInfo: IMenuInfo[] = [
        {
            choice: "Dashboard",
        **component: BacklogItemDashboard,**  // Red squigglies.
            destination: "dashboard".toLowerCase().replace(/ /g, ""),
        },
        ... [more menus deleted for brevity]
   ]

    const routes = menuInfo.map((info: IMenuInfo, key: number) => {
        return (
            <div key={key}>
                <Route
                    path={`${path}/${info.destination}`}
                    **component={info.component}** // another red squiggly
                />
            </div>
        );
    });

    return (
        <div>
            <ServiceItemContent
                menu={
                    <ServiceItemMenu
                        service="Backlog"
                        id={id}
                        choices={menuInfo}
                    />
                }
            >
                {routes}
            </ServiceItemContent>
        </div>
    );
};

Question: What type should "component" have in the interface definition to get rid of the two red squigglies?问题:“组件”在接口定义中应该有什么类型才能去掉两个红色的波浪线? Tried ReactNode as well but still red squiggles.也尝试过 ReactNode,但仍然是红色曲线。 Tried also Component but the same.也尝试过 Component 但相同。

New to programming.编程新手。 New to React. React 新手。 New to JavaScript. JavaScript 的新功能。 Have dream application trying to make it happen.有梦想的应用程序试图实现它。 Head hurts from no-understand/luck Google and beating head against wall.不理解/运气谷歌导致头部受伤,头部撞墙。

Just need a quick fix--other than unknown or any--and if you have them links to SO posts that I'll study over the weekend since I'm sure this is basic enough to have been asked and answered already.只需要一个快速修复——除了未知或任何——如果你有它们链接到我将在周末学习的 SO 帖子,因为我确信这已经足够基本,已经被问到和回答了。 Did I give enough code to understand the problem?我是否提供了足够的代码来理解问题? Thanks.谢谢。

Note: I specifically don't want to use React.FC due to this and similar articles: https://fettblog.eu/typescript-react-why-i-dont-use-react-fc/ .注意:由于这篇文章和类似文章,我特别不想使用 React.FC: https://fettblog.eu/typescript-react-why-i-dont-use-react-fc/

Jason杰森

BacklogItemDashboard is a function that returns a ReactElement . BacklogItemDashboard是一个 function返回一个ReactElement It is not a ReactElement itself.它本身不是ReactElement

Any of the following types describe a component which fixes your errors.以下任何类型都描述了修复错误的组件。 Meaning that BacklogItemDashboard is assignable to this type and this type is assignable to the component prop of the Route component.这意味着BacklogItemDashboard可分配给此类型,并且此类型可分配给Route组件的component prop。

  • () => ReactElement - a function which takes no argument and returns a React element. () => ReactElement - 一个 function 不带参数并返回一个 React 元素。

  • () => ReactElement | null () => ReactElement | null - a function which takes no argument and returns a React element or null. () => ReactElement | null - 一个 function 不带参数并返回一个 React 元素或 null。

  • React.FC - a function which takes no arguments except children and returns a React element or null. React.FC - 一个 function,它不接受 arguments,除了子元素并返回一个 React 元素或 null。

  • React.ComponentType - a function component or class component that takes no arguments except maybe children React.ComponentType - 一个 function 组件或 class 组件,不带 arguments 可能除了孩子

  • React.FC<RouteComponentProps> - a function component that takes the props provided by the Route React.FC<RouteComponentProps> - 一个 function 组件,它采用 Route 提供的道具

  • React.ComponentType<RouteComponentProps> - a function component or class component that takes the props provided by the Router React.ComponentType<RouteComponentProps> - 一个 function 组件或 class 组件,它采用路由器提供的道具

I actually agree with the general premise of the "Why I Don't Use React.FC" article that you should type function arguments rather than the functions themselves when writing the function .我实际上同意“为什么我不使用 React.FC”文章的一般前提,即您应该在编写 ZC1C425268E68385D1AB5074C17A94F1 时键入 function arguments 而不是函数本身I don't think that applies when you are talking about declaring the type for a prop which is a function, like component .当您谈论声明 function 的道具类型时,我认为这并不适用,例如component I don't see any problem with using React.FC here.我认为在这里使用React.FC没有任何问题。 You don't need to change the type of BacklogItemDashboard as () => ReactElement is assignable to React.FC .您不需要更改BacklogItemDashboard的类型,因为() => ReactElement可以分配给React.FC But if you are opposed, you can go with () => ReactElement .但是如果你反对,你可以用() => ReactElement

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM