React18中 children 属性已经在函数组件中被移除,如果需要使用 children 属性,则需要手动声明。

第一种方法,手动声明 children 属性。

1
2
3
4
5
import * as React from 'react';
type Props = {
children?: React.ReactNode
};
const Component: React.FC<Props> = ({children}) => {...}

第二种方法,使用 PropsWithChildren 辅助属性。

1
2
3
import React, { PropsWithChildren } from 'react';
type Props = {};
const Component: React.FC<PropsWithChildren<Props>> = ({children}) => {...}

//React18中props删除了children属性,当我们的在ts环境下使用props.children会报错
//定义一个Iprops的interface,指定children属性,因为children是可有可无的,所以设置为可选属性,类型为React.ReactNode

1
2
3
interface IProps {
children?: React.ReactNode;
}