简体   繁体   English

类型“{}”上不存在属性“children”

[英]Property 'children' does not exist on type '{}'

I'm having a typescript error.我有一个 typescript 错误。 It says that 'children' does not exist on type '{}' even though this syntax works on my other projects.它说 'children' 在类型 '{}' 上不存在,即使这种语法适用于我的其他项目。

I'm guessing this new app is on React 18.我猜这个新应用程序是基于 React 18 的。

React 18 removed children from the FC type. React 18 从FC类型中移除了children If you want it back you need to add it to the props yourself.如果你想要它回来,你需要自己将它添加到道具中。

const Foo: React.FC<{ children: React.ReactNode }> = ({ children }) => <>{children}</>

Or preferably, don't use the FC type at all:或者最好根本不使用FC类型:

interface Props {
    children: React.ReactNode
}

function Foo({ children }: Props) {
    return<>{children}</>
}

You have not defined a type for React.FC您还没有为React.FC定义类型

The fix could be修复可能是

type Props = {
   children: React.ReactNode
}

const Page: React.FC<Props> = ({ children }) => {
  ...
}

As mentioned by others, React 18 removed children from the props type definition.正如其他人所提到的,React 18 从 props 类型定义中删除了children

You can do the following instead, by explicitly declaring your props should include children:您可以通过明确声明您的道具应包括孩子来执行以下操作:

import { FunctionComponent, PropsWithChildren } from 'react';

export const MyComponent: FunctionComponent<PropsWithChildren> =
  ({ children }) => <div>{children}</div>;

The above would default the props to the unknown type.以上将道具默认为unknown类型。

You can also define the props:您还可以定义道具:

import { FunctionComponent, PropsWithChildren } from 'react';

interface Props {
  label: string;
}

export const MyComponent: FunctionComponent<PropsWithChildren<Props>> =
  ({ label, children }) => <div>{label}: {children}</div>;

Or even better:或者更好:

import { FunctionComponent, PropsWithChildren } from 'react';

interface Props extends PropsWithChildren {
  label: string;
}

export const MyComponent: FunctionComponent<Props> =
  ({ label, children }) => <div>{label}: {children}</div>;

You need to replace the destructured props argument by following您需要通过以下方式替换 destructured props 参数

{ children }: {children: React.ReactNode}

暂无
暂无

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

相关问题 类型 '{ children?: ReactNode; 上不存在属性 'onClick' }' - Property 'onClick' does not exist on type '{ children?: ReactNode; }' 属性不存在于类型 'IntrinsicAttributes & { children?: ReactNode; }' - Property does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }' "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' SlateJS Typscript node.children url 和 type 属性不存在 - SlateJS Typscript node.children url and type property does not exist 类型 '{ children?: ReactNode; 上不存在属性 'firebase' }' 如何正确注释? - Property 'firebase' does not exist on type '{ children?: ReactNode; }' how to propperly annotate? ts(2322) 属性子项在类型“内在属性和道具”上不存在 - ts(2322) Property children does not exist on type 'Intrinsic attributes and Props' 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' React Antd 模态属性 &#39;children&#39; 在类型&#39;IntrinsicAttributes &amp; ModalProps&#39; 上不存在 - React Antd Modal Property 'children' does not exist on type 'IntrinsicAttributes & ModalProps' 类型'IntrinsicAttributes &amp; PhoneInputProps &amp; { children?: ReactNode; 上不存在属性'ref' } - Property 'ref' does not exist on type 'IntrinsicAttributes & PhoneInputProps & { children?: ReactNode; } React:如何解决:“IntrinsicAttributes & Props”类型上不存在属性“children” - React: How to solve: Property 'children' does not exist on type 'IntrinsicAttributes & Props'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM