首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型“{}”上不存在属性“子”

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

Stack Overflow用户
提问于 2022-04-21 03:54:19
回答 4查看 6.4K关注 0票数 6

我有打字错误。它说‘子“不存在于'{}’类型上,即使这个语法在我的其他项目上有效。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-04-21 03:58:24

您还没有为React.FC定义类型

解决办法可能是

代码语言:javascript
复制
type Props = {
   children: React.ReactNode
}

const Page: React.FC<Props> = ({ children }) => {
  ...
}
票数 6
EN

Stack Overflow用户

发布于 2022-04-21 04:04:02

我猜这款新的应用程序已经启动了18。

将18 childrenFC类型中删除。如果你想要拿回它,你需要自己把它添加到道具中。

代码语言:javascript
复制
const Foo: React.FC<{ children: React.ReactNode }> = ({ children }) => <>{children}</>

或者最好不要使用FC类型:

代码语言:javascript
复制
interface Props {
    children: React.ReactNode
}

function Foo({ children }: Props) {
    return<>{children}</>
}
票数 18
EN

Stack Overflow用户

发布于 2022-06-22 01:15:45

正如其他人所提到的,React 18从道具类型定义中删除了children

您可以通过显式声明您的道具应该包括子元素来执行以下操作:

代码语言:javascript
复制
import { FunctionComponent, PropsWithChildren } from 'react';

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

以上将默认为unknown类型的支持。

您还可以定义道具:

代码语言:javascript
复制
import { FunctionComponent, PropsWithChildren } from 'react';

interface Props {
  label: string;
}

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

甚至更好:

代码语言:javascript
复制
import { FunctionComponent, PropsWithChildren } from 'react';

interface Props extends PropsWithChildren {
  label: string;
}

export const MyComponent: FunctionComponent<Props> =
  ({ label, children }) => <div>{label}: {children}</div>;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71948755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档