简体   繁体   English

输入 '({ items }: PropsWithChildren<todoprops> ) => Element[]' 不可分配给类型 'FunctionComponent<todoprops> '</todoprops></todoprops>

[英]Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>'

I'm learning Typescript-react and I'm stuck in this error Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>' and I am lost on this.我正在学习 Typescript-react 并且我陷入了这个错误Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>'我迷失了.

Complete error:完整错误:

Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>'.
  Type 'Element[]' is missing the following properties from type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>': type, props, key

Link for code: sandbox repo .代码链接: sandbox repo

Error happens on the declaration of TodoList function within the TodoList.tsx file. TodoList.tsx文件中的TodoList function 声明发生错误。

Any help is appreciated.任何帮助表示赞赏。 Cheers!干杯!


Code:代码:

import React from "react";

interface Todo {
  id: number;
  content: string;
  completed: boolean;
}

interface TodoProps {
  items: Todo[];
}

//    v------v here is error
const TodoList: React.FC<TodoProps> = ({ items }) => {
  return items.map((item: Todo) => <div key={item.id}>{item.id}</div>);
};

export default TodoList;

Yeah, the error may sound a bit confusing - in essence it says that you can only return a single ReactElement or its equivalent JSX.Element in the function component definition, enforced by React.FC type.是的,这个错误可能听起来有点令人困惑——本质上它表示你只能在 function 组件定义中返回单个ReactElement或其等效的JSX.Element ,由React.FC类型强制执行。

React Fragments solve this limitation, so you can write TodoList in the following manner : React Fragments 解决了这个限制,所以你可以用以下方式编写TodoList

interface TodoProps {
  items: Todo[];
}

const TodoList: React.FC<TodoProps> = ({ items }) => (
  <React.Fragment>
    {items.map((item: Todo) => (
      <div key={item.id}>{item.id}</div>
    ))}
  </React.Fragment>
);
Short form: 简写:
 const TodoList: React.FC<TodoProps> = ({ items }) => ( <> {items.map(({ id }) => <div key={id}>{id}</div>)} </> );

By the way: With pure JS, both class and function components can return multiple elements in an array as render output.顺便说一句:使用纯 JS,class 和 function 组件都可以返回数组中的多个元素作为渲染 output。 Currently, TS has a type incompatibility for returned arrays in function components, so Fragments provide a viable workaround here (in addition to type assertions ).目前,TS 在 function 组件中返回的 arrays 存在类型不兼容,因此 Fragments 在这里提供了可行的解决方法(除了类型断言)。

I've encountered a similar error.我遇到了类似的错误。 Eventually I noticed that I'd renamed the file incorrectly from.js to.ts instead of to.tsx when converting a component to a FunctionComponent with TypeScript.最终,我注意到在使用 TypeScript 将组件转换为 FunctionComponent 时,我错误地将文件从.js 重命名为 to.ts 而不是 to.tsx。

I also got this error when I was trying to return children props from my Loading component like below.当我尝试从如下所示的Loading组件返回children道具时,我也遇到了这个错误。


    const { loading, children } = props;
    return loading ? <p>Loading ... </p> : children;

Then i realize that React is expecting only one return value(1 parent component) from its render method.然后我意识到React 只期望它的render方法有一个返回值(1 个父组件)。 Therefore I wrapped children props with React.Fragment which is denoted by <></> and that solves my problem.因此,我用<></>表示的React.Fragment包裹了儿童道具,这解决了我的问题。 Below is my Loading component sample, hope that helps someone else.下面是我的Loading组件示例,希望对其他人有所帮助。

import { FunctionComponent } from "react";

interface ILoadingProps {
  loading: boolean;
}
export const Loading: FunctionComponent<ILoadingProps> = (props) => {
  const { loading, children } = props;
  return loading ? <p>Loading ... </p> : <>{children}</>;
};

My problem was that I had allowed a "TypeScript Quick Fix" in VSCode to add async to the main functional component interface.我的问题是我允许 VSCode 中的“TypeScript Quick Fix”将异步添加到主要功能组件界面。

const Welcome: React.FC<TProps> = async (props) => {

After removing it,删除后,

const Welcome: React.FC<TProps> = (props) => {

things were back to normal.一切恢复正常。

暂无
暂无

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

相关问题 Typescript 和 React:类型“Element”不可分配给类型“FunctionComponent&lt;{}&gt;” - Typescript and React: Type 'Element' is not assignable to type 'FunctionComponent<{}>' 类型 &#39;(props: RouteProps) =&gt; JSX.Element&#39; 不可分配给类型 &#39;PropsWithChildren<RouteProps> &#39; - Type '(props: RouteProps) => JSX.Element' is not assignable to type 'PropsWithChildren<RouteProps>' JSX.Element' 不可分配给类型 'FunctionComponent&lt;{}&gt;' - JSX.Element' is not assignable to type 'FunctionComponent<{}>' 类型 &#39;(props: Props) =&gt; Element[]&#39; 不可分配给类型 &#39;FunctionComponent<Props> &#39; - Type '(props: Props) => Element[]' is not assignable to type 'FunctionComponent<Props>' React 的 Typescript 编译错误 - 类型“Element”不可分配给类型“FunctionComponent&lt;{}&gt;” - Typescript Compilation Error with React - Type 'Element' is not assignable to type 'FunctionComponent<{}>' React TypeScript: (props: OwnProps) =&gt; Element' 不可分配给类型 'FunctionComponent&lt;{}&gt; - React TypeScript: (props: OwnProps) => Element' is not assignable to type 'FunctionComponent<{}> “tech”类型的参数不能分配给“PropsWithChildren”类型的参数<props> '</props> - Argument of type '“tech”' is not assignable to parameter of type 'PropsWithChildren<Props>' 类型&#39;(props:Props)=&gt;元素&#39;不能分配给&#39;FunctionComponent类型 <FieldRenderProps<any, HTMLElement> &gt;&#39;与React-final-form - Type '(props: Props) => Element' is not assignable to type 'FunctionComponent<FieldRenderProps<any, HTMLElement>>' with React-final-form 输入'功能组件<svgattributes<svgelement> >' 不可分配给类型 'string'.ts </svgattributes<svgelement> - Type 'FunctionComponent<SVGAttributes<SVGElement>>' is not assignable to type 'string'.ts TS 错误:类型“未知”不可分配给类型“FunctionComponent”<any> 由于带路由器 - TS error: Type 'unknown' is not assignable to type 'FunctionComponent<any> due to withRouter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM