0

In React, I can reference any component function, which is really helpful for cases where I want to conditionally use one component type over another. For example:

const Link = ({ text, href, external = false }) => {
  const LinkType = external ? ExternalLink : InternalLink

  return <LinkType href={href}>{text}</LinkType>
}

What I can't figure out is, how can I implement this same type of interface when my child components are simple HTML elements? Is there something like this?

const SomeComponent = external ? React.Component('div') : React.Component('span')
bloudermilk
  • 17,820
  • 15
  • 68
  • 98

1 Answers1

-3

Just like this:

const SomeComponent = external ? <div>Hey!</div> : <span>Hey!</span>;
enbermudas
  • 1,603
  • 4
  • 20
  • 42
  • 2
    This won't do the same thing as conditionally using a component *reference*. In other words, it removes the capability of doing ``, which is what the OP is doing. – Brian Thompson Apr 20 '21 at 13:09