Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  57] [ 3]  / answers: 1 / hits: 61348  / 6 Years ago, fri, november 23, 2018, 12:00:00

I'm trying to pass a variable of type React.Component (or React.FunctionComponent) into a Route, like this:



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. [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.


More From » reactjs

 Answers
35

I have encountered this a couple of times. Try these:



  1. Type your PrivateRoute as React.FC<Props>

  2. Type your incoming component as React.ElementType


The ultimate truth about React types comes from the docs


Edit: React.ReactType (deprecated) -> React.ElementType


[#53050] Tuesday, November 20, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denver

Total Points: 232
Total Questions: 111
Total Answers: 103

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
;