The children
is a prop like any other and can be of any type. It's only special insofar that child JSX elements are automatically mapped to the children
prop.
So, while it's usually declared as children?: React.ReactNode
, you could declare it as a render prop, or even as a custom type like so:
interface INameProps {
children: {
fistName: string,
lastName: string
}
}
const Name: React.FC<INameProps> = ({children}) => {
return <div>{children.fistName} {children.lastName}</div>;
}
And then you can use it like so:
<Name>
{
{
fistName: "John",
lastName: "Smith"
}
}
</Name>
Which is the same as:
<Name
children={
{
fistName: "John",
lastName: "Smith"
}
}
/>
React.ReactNode
– MessmateReact.PropsWithChildren<MyProps>
. That adds the default children to any props. – Millar