Reputation: 25
I want to pass in a component props named as.
As will be bound to html tags and "link" string .
If it's a link I want it to return the Link component of react-router-dom
In the remaining cases, whatever name tag is passed, it will be rendered.
For example :
as="div" -> render <div></div>
as="span" -> render <span></span>
as="link" -> render <Link></Link>
Can you help me
Upvotes: 1
Views: 37
Reputation: 24651
const Component = ({ as, children }) => {
if (as === "link") return <Link>{children}</Link>;
const Comp = as;
return <Comp>{children}</Comp>;
};
Working example:
Upvotes: 1