I want to create a, b, div or another element based on as={""} prop in my function.
I am checking as and then use if guards to create element one-by-one. But is there any way to create html element based on as component?
I want to be able to create b, a, h4 h3 or any element without if's
I've found similar question but it uses styled components. Is it possible to do it without it?
Currently im doing this:
import * as React from 'react'
interface Props {
children: (React.ReactChild | React.ReactNode) | (React.ReactChild[] | React.ReactNode[])
onClick?: (e) => void
as?: string
}
const DynElement: React.FunctionComponent<Props> = props => {
const { as , children } = props
return (
<>
{as === 'div' && <div {...props_rest}>
{children}
</div>}
{as === 'b' && <b {...props_rest}>
{children}
</b>}
</>
)
}
export default DynElement
and usage is:
<DynElement as="b" onClick={...}>...</DynElement>
Using TypeScript + React.