gpt4 book ai didi

reactjs - 我应该为 child 的 TypeScript Prop 使用什么类型?

转载 作者:行者123 更新时间:2023-12-04 08:42:11 25 4
gpt4 key购买 nike

我正在做:

export default function PageTitle(props: ReactElement) {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
)
}

但我收到投诉:
Type error: Property 'children' does not exist on type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)>) | (new (props: any) => Component<any, any, any>)>'
什么是正确使用的类型?

最佳答案

我会这样写

   type Props = {
children: string;
};

const PageTitle : React.FC<Props> = (props) => {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
)
}

export default PageTitle;
或者像这样
type Props = {
children: string;
};

const PageTitle = ({children, ...props}: Props) => {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{children}</Typography>
)
}

export default PageTitle;

关于reactjs - 我应该为 child 的 TypeScript Prop 使用什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com