
我有打字错误。它说‘子“不存在于'{}’类型上,即使这个语法在我的其他项目上有效。
发布于 2022-04-21 03:58:24
您还没有为React.FC定义类型
解决办法可能是
type Props = {
children: React.ReactNode
}
const Page: React.FC<Props> = ({ children }) => {
...
}发布于 2022-04-21 04:04:02
我猜这款新的应用程序已经启动了18。
将18 children从FC类型中删除。如果你想要拿回它,你需要自己把它添加到道具中。
const Foo: React.FC<{ children: React.ReactNode }> = ({ children }) => <>{children}</>或者最好不要使用FC类型:
interface Props {
children: React.ReactNode
}
function Foo({ children }: Props) {
return<>{children}</>
}发布于 2022-06-22 01:15:45
正如其他人所提到的,React 18从道具类型定义中删除了children。
您可以通过显式声明您的道具应该包括子元素来执行以下操作:
import { FunctionComponent, PropsWithChildren } from 'react';
export const MyComponent: FunctionComponent<PropsWithChildren> =
({ children }) => <div>{children}</div>;以上将默认为unknown类型的支持。
您还可以定义道具:
import { FunctionComponent, PropsWithChildren } from 'react';
interface Props {
label: string;
}
export const MyComponent: FunctionComponent<PropsWithChildren<Props>> =
({ label, children }) => <div>{label}: {children}</div>;甚至更好:
import { FunctionComponent, PropsWithChildren } from 'react';
interface Props extends PropsWithChildren {
label: string;
}
export const MyComponent: FunctionComponent<Props> =
({ label, children }) => <div>{label}: {children}</div>;https://stackoverflow.com/questions/71948755
复制相似问题