reactjs - Vilken typ ska jag använda för TypeScript-props med children?
Jag gör:
export default function PageTitle(props: ReactElement) {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
)
}
Men jag får ett klagomål:
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>)>'
Vilken typ är rätt att använda?
Svar #1
Jag kommer att skriva det så här
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;
eller så här
type Props = {
children: string;
};
const PageTitle = ({children, ...props}: Props) => {
return (
<Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{children}</Typography>
)
}
export default PageTitle;
