简体   繁体   English

TypeScript 3:JSX 元素类型“组件”没有任何构造或调用签名。 [2604]

[英]TypeScript 3: JSX element type 'Component' does not have any construct or call signatures. [2604]

I'm trying to pass a variable of type React.Component (or React.FunctionComponent) into a Route, like this:我正在尝试将 React.Component (或 React.FunctionComponent)类型的变量传递到 Route 中,如下所示:

import React from 'react';
import { Route } from 'react-router-dom';

type PrivateRouteProps = {
  component: React.Component | React.FunctionComponent;
  isAuthenticated: boolean;
  login: (...args: any[]) => any;
  path: string;
};

const PrivateRoute: React.FunctionComponent<PrivateRouteProps> = ({
  component: Component,
  isAuthenticated,
  login,
  path,
  ...rest
}) => {
  return (
    <Route
      path={path}
      {...rest}
      render={props => {
        if (isAuthenticated) {
          return <Component {...props} />;
        } else {
          login();
          return null;
        }
      }}
    />
  );
};

But I'm getting this error:但我收到此错误:

JSX element type 'Component' does not have any construct or call signatures. JSX 元素类型“组件”没有任何构造或调用签名。 [2604] [2604]

I've read through a bunch of other threads about this issue, but they all seem to deal with this error coming up for a specific component implementation.我已经阅读了很多关于这个问题的其他线程,但它们似乎都处理了针对特定组件实现出现的这个错误。 I can't change the component in question or import it differently (like the accepted answers often suggest), because it could be any component.我无法更改有问题的组件或以不同方式导入它(就像公认的答案通常建议的那样),因为它可以是任何组件。

I'm using TypeScript 3.1.6, Babel Core 7.1, and React 16.6.3.我使用的是 TypeScript 3.1.6、Babel Core 7.1 和 React 16.6.3。

Late to the party, with "@types/react-router-dom": "^4.3.4" and "@types/react": "16.9.1" , and if you're using RouteProps , you will probably get the same error.聚会迟到了, "@types/react-router-dom": "^4.3.4""@types/react": "16.9.1" ,如果你使用RouteProps ,你可能会得到同样的错误。

JSX element type 'Component' does not have any construct or call signatures. JSX 元素类型“组件”没有任何构造或调用签名。 [2604] [2604]

That's because, in the RouteProps interface, the component is defined as optional, hence it might be undefined.这是因为,在RouteProps接口中, component被定义为可选的,因此它可能是未定义的。

export interface RouteProps {
  location?: H.Location;
  component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
  render?: ((props: RouteComponentProps<any>) => React.ReactNode);
  children?: ((props: RouteChildrenProps<any>) => React.ReactNode) | React.ReactNode;
  path?: string | string[];
  exact?: boolean;
  sensitive?: boolean;
  strict?: boolean;
}

Simply check for if the component is falsy will fix it.只需检查component是否为假就可以修复它。

function PrivateRoute({ component: Component, ...rest }: RouteProps) {
  if (!Component) return null;
  return (
    <Route
      {...rest}
      render={props =>
        fakeAuth.isAuthenticated ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: props.location }
            }}
          />
        )
      }
    />
  );
}

I have encountered this a couple of times.我遇到过几次这种情况。 Try these:试试这些:

  1. Type your PrivateRoute as React.FC<Props>键入您的PrivateRoute作为React.FC<Props>
  2. Type your incoming component as React.ElementType将传入的组件输入为React.ElementType

The ultimate truth about React types comes from the docs React 类型的最终真相来自文档

Edit: React.ReactType (deprecated) -> React.ElementType编辑:React.ReactType(不推荐使用)-> React.ElementType

Even later to the party, but what worked for me is this:甚至后来参加聚会,但对我有用的是:

 interface PrivateRouteProps extends Omit<RouteProps, "component"> { component: React.ElementType; // any additional vars } PrivateRoute: React.FC<PrivateRouteProps> = ({ component: Component, ...rest }) => { // render code }

This is late but in case someone didn't want a solution but an explanation, let's talk about this error using an example to demonstrate it这已经晚了,但如果有人不想要解决方案而是需要解释,让我们用一个例子来讨论这个错误来演示它

function PaymentPage(){
  
   return <div>Payment Page</div>
}

say we want to create a dynamic payment form, if the query parameter is with=stripe so we assume he wants to pay with stripe, if it's with razorpay we assume it, ...etc.假设我们想创建一个动态支付表单,如果查询参数是 with=stripe 所以我们假设他想用 stripe 支付,如果是用 razorpay 我们假设它,......等等。

we then do something like然后我们做类似的事情

function PaymentPage(){
   const router = useRouter;
   const {with_} = router.query;
   let GatewayComponent: Gateway | null = null;
   switch(with_){
     case 'stripe':
       GatewayComponent = <StripeGateway/>;
       break;
     case 'razorpay':
       GatewayComponent = <RazorpayGateway/>;
       break;
   }
   return <GatewayComponent/>
}

Running this, we get运行这个,我们得到

JSX element type 'Component' does not have any construct or call signatures.

What happens?发生什么了?

What are component?什么是组件?

  • Constructors that return elements of type JSX.Element返回 JSX.Element 类型元素的构造函数

So?所以?

  • We are not returning a constructor , we are returning a Constructor call , it's the same as assuming that GatewayComponent is a constructor, but it's not, it's a variable holding JSX我们不是返回一个构造函数,我们返回的是一个构造函数调用,这与假设GatewayComponent是一个构造函数是一样的,但它不是,它是一个包含 JSX 的变量

So basically, expects x to be a constructor of any type, either a function or a class, if it's a function, the function is the render function, if it's a class, it needs a render method.所以基本上,期望 x 是任何类型的构造函数,无论是函数还是类,如果是函数,则函数是渲染函数,如果是类,则需要渲染方法。

Back to our problem回到我们的问题

function PaymentPage(){
   const router = useRouter;
   const {with_} = router.query;
   let gateway: Gateway | null = null;
   switch(with_){
     case 'stripe':
       gateway = <StripeGateway/>;
       break;
     case 'razorpay':
       gateway = <RazorpayGateway/>
       break;
   }
   return <React.Fragment> {gateway} </React.Fragment>
}

Because gateway holds JSX, not a constructor that returns JSX因为网关持有 JSX,而不是返回 JSX 的构造函数

What if you want to use it as a component?如果您想将其用作组件怎么办?

function PaymentPage(){
   const router = useRouter;
   const {with} = router.query;
   let GatewayComponent: Gateway | null = null;
   switch(with_){
     case 'stripe':
       return () => <StripeGateway/>
     case 'razorpay':
       return () => <RazorpayGateway/>
   }
   return <GatewayComponent/>
}

Now it's a constructor, we can use it as a component right now.现在它是一个构造函数,我们现在可以将它用作组件。

Formally , you pass the constructor and not the instance.正式地,您传递的是构造函数而不是实例。

<PrivateRoute path="'/> <PrivateRoute path="'/>
Path gave an error while using interface PrivateRouteProps then i switched it to路径在使用接口 PrivateRouteProps 时出错,然后我将其切换到

export type PrivateRouteProps = {
  component: React.ElementType;
  currentUser?: any;
} & RouteProps;```

Final result below最终结果如下

import { Route, Redirect, RouteProps } from "react-router-dom";
export type PrivateRouteProps = {
  component: React.ElementType;
  currentUser?: any;
} & RouteProps;

const PrivateRoute: React.FC<PrivateRouteProps> = ({
  component: Component,
  currentUser,
  ...rest
}) => (
  <Route
    {...rest}
    render={(props) =>
      currentUser ? <Component {...props} /> : <Redirect to="/" />
    }
  />
);

I was able to resolve the error by typing my component as follows:我能够通过按如下方式键入我的组件来解决错误:

...
export function withApollo (PageComponent: () => JSX.Element) {
const WithApollo = <T extends object>(props: T) => {
...

暂无
暂无

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

相关问题 打字稿类型定义:TS2604:JSX元素类型“某物”没有任何构造或调用签名 - Typescript type definition: TS2604: JSX element type 'something' does not have any construct or call signatures JSX 元素类型“GoogleMapReact”没有任何构造或调用 signatures.ts(2604) - JSX element type 'GoogleMapReact' does not have any construct or call signatures.ts(2604) TS2604:JSX元素类型“没有任何构造或调用签名 - TS2604: JSX elemy type '' does not have any construct or call signatures JSX 元素类型“x”没有任何构造或调用签名 - JSX element type 'x' does not have any construct or call signatures TypeScript 反应 - JSX 元素类型“Modal.Header”没有任何构造或调用签名 - TypeScript react - JSX element type 'Modal.Header' does not have any construct or call signatures 错误 JSX 元素类型‘Header’没有任何构造或调用签名”是什么意思? - What does the error JSX element type 'Header' does not have any construct or call signatures" mean? JSX 元素类型 'Route' 没有任何构造或调用签名 - 路由问题 - JSX element type 'Route' does not have any construct or call signatures - route problem 为什么 JSX 元素类型“x”没有任何构造或调用签名 - why JSX element type 'x' does not have any construct or call signatures 尝试迭代嵌套 arrays 的多个级别不断得到“没有任何构造或调用签名” - Trying to iterate over several levels of nested arrays keep getting 'does not have any construct or call signatures' 此表达式不可调用。 “布尔”类型没有调用签名。 Vue3计算属性错误 - This expression is not callable. Type 'Boolean' has no call signatures. Error in Vue3 computed property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM