简体   繁体   English

Typescript 和 React:类型“Element”不可分配给类型“FunctionComponent<{}>”

[英]Typescript and React: Type 'Element' is not assignable to type 'FunctionComponent<{}>'

I am converting a react project over to typescript and I'm having an issue.我正在将 React 项目转换为打字稿,但遇到了问题。 The scenario is this: I have a series of Chapter components.场景是这样的:我有一系列Chapter组件。 Each Chapter is associated with its own unique data and unique Map component.每个Chapter都与自己独特的数据和独特的Map组件相关联。 Each Chapter renders a ChapterTemplate , and passes the Map as a prop:每个Chapter渲染一个ChapterTemplate ,并将Map作为道具传递:

// Chapter.tsx

import model from './modal';
import Map from './Map';

type ChapterProps = {
    data: ModelSchema;
    map: ReactElement;
};

const Chapter: FunctionComponent<ChapterProps> = () => {
    return <ChapterTemplate data={model} map={<Map />} />;
};

This is giving me the following ts error at map={<Map />} :这在map={<Map />}给了我以下 ts 错误:

Type 'Element' is not assignable to type 'FunctionComponent<{}>'. Type 'Element' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any, any>'.

I am not sure how to type this prop based on how I am using it.我不确定如何根据我的使用方式输入这个道具。

For some context, here is what a <Map /> component should look like:对于某些上下文, <Map />组件应该如下所示:

type MapProps = {
    metadata?: {
        name: string;
        theme: string;
    };
    mapState?: {
        center: number[];
        zoom: number;
        basemap: any;
        layers: any[];
    };
};

const Map = ({
    metadata,
    mapState: { basemap, layers, zoom, center },
}: MapProps) => {
  
  // ... custom logic here for each chapter ...

}

Ultimately it is the ChapterTemplate component that organizes the data, manages the state, and passes certain pieces of data to the Map as props using React.cloneElement :最终是ChapterTemplate组件组织数据,管理状态,并使用React.cloneElement将某些数据作为道具传递给Map

const Chapter = ({
  map,
  data: { pages, metadata },
}: ChapterProps) => {
  const [currentPage, setCurrentPage] = useState(0);
  const Map = map;

  return (
    <Wrapper>
      // Some other stuff in here
      <MapContainer>
        {cloneElement(map as null, {
          mapState: pages[currentPage].mapState,
          metadata,
        })}
      </MapContainer>
    </Wrapper>
  );
};

I read the question How to assign the correct typing to React.cloneElement when giving properties to children?我阅读了问题如何在为子项提供属性时为 React.cloneElement 分配正确的类型? , which is where I got the idea to use ReactElement as the type for my map prop. ,这就是我想到使用ReactElement作为我的map道具类型的地方。 I tried using map as ReactElement<any> in my cloneElement call but it was still giving me errors, so I gave up and put in as null for now.我尝试在我的cloneElement调用中使用map as ReactElement<any>但它仍然给我错误,所以我现在放弃并输入as null But I am still getting this error.但我仍然收到此错误。 For the record, the code does work and compile - react is happy with my syntax and organization.作为记录,代码确实可以工作和编译 - react 对我的语法和组织很满意。 But is typescript trying to tell me something about the way I'm using a component as a prop?但是打字稿是否试图告诉我一些关于我使用组件作为道具的方式? Is there a more proper way to do this?有没有更合适的方法来做到这一点? Thanks for reading.谢谢阅读。

Check the code here , it's the issue of mismatching type ReactElement vs FunctionalComponent检查这里的代码,这是ReactElementFunctionalComponent类型不匹配的问题

Update:更新:

Hey can you change it to嘿,你能把它改成

const ChapterTemplate = ({
    data,
    map,
}: { data: any, map: FunctionComponent<MapProps>}) => { // change this to ReactElement
  return (<div>Hello world</div>);
}

const Chapter: FunctionComponent<ChapterProps> = (props) => {
    return <ChapterTemplate data={props.data} map={Map} />;
};

and then do the <Map />然后做<Map />

or do this或者这样做

const ChapterTemplate = ({
    data,
    map,
}: { data: any, map: ReactElement}) => { // change this to ReactElement
  return (<div>Hello world</div>);
}

const Chapter: FunctionComponent<ChapterProps> = (props) => {
    return <ChapterTemplate data={props.data} map={<Map />} />;
};

暂无
暂无

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

相关问题 获取“类型不可分配给类型 FunctionComponent<props> “和”类型缺少类型“React Element”中的以下属性<any, any> '</any,></props> - Getting a "Type is not assignable to type FunctionComponent<Props>" and "Type is missing the following properties from type 'React Element<any, any>' React/TypeScript - 类型 'false | 元素'不可分配给类型'元素' - React/TypeScript - Type 'false | Element' is not assignable to type 'Element' React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type React Typescript不可分配给type参数 - React typescript is not assignable to parameter of type TypeScript - 类型“Element”不可分配给类型“ReactChildren” - TypeScript - Type 'Element' is not assignable to type 'ReactChildren' 与 TypeScript 反应:类型不可分配给类型“IntrinsicAttributes” - React with TypeScript: Type is not assignable to type 'IntrinsicAttributes' 无效类型不能分配给布尔类型(反应/打字稿) - Type Void is Not Assignable to Type Boolean (React/Typescript) TS 错误:类型“未知”不可分配给类型“FunctionComponent”<any> 由于带路由器 - TS error: Type 'unknown' is not assignable to type 'FunctionComponent<any> due to withRouter 打字稿:不可分配给键入错误 - Typescript: is not assignable to type error TypeScript:类型参数不可分配 - TypeScript: Argument of type is not assignable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM