I have a component where I receive props that include a Link component (of React router, but could be anything):
children: [
{
id: '1',
isActive: false,
label: 'Forms',
icon: <DestinationsIcon height={30} width={30} />,
link: <Link to={'somewhere'} />,
},
Then in the component receiving those props I can extract the props form the children:
<List>
{React.Children.map(children, (child) => {
if (!React.isValidElement(child)) {
return null;
}
return React.cloneElement(child, {
children: <div>{child.props.label}</div>,
});
})}
</List>
The child.props.label does return the correct label for example. However what I'd like to do is replace that wrapping <div> with the link props, something like this:
children: <child.props.link>{child.props.label}</child.props.link>,
This of course doesn't work. If I'm trying with React.createElement:
children: React.createElement(child.props.link),
The first argument should be a string, but I'm passing a component. The thing is that I must receive the Link component from outside with its own context, and I cannot re-create it, otherwise I could avoid all the complexity.
Does anyone know what would be the correct way to render that link props as a JSX wrapper?