Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1774

How to pass a component constructor as a prop

$
0
0

Amazing, thank you so much! I’ll definitely need to look more into first class modules to understand how this actually works. But I was able to get optional icons working based off the code you provided!

@react.component
let make = (~variant=Variant.Success, ~label="", ~icon: option<module(Lucide.Icon)>=?) => {
  let icon = switch icon {
  | Some(i) => {
      module Icon = unpack(i)
      <Icon size={16} />
    }
  | None => React.null
  }

  <div>
    {icon}
    {React.string(label)}
  </div>
}

Edit: An even simpler version

module Empty = {
  @react.component(: Lucide.Props.t)
  let make = () => {
    React.null
  }
}

@react.component
let make = (
  ~variant=Variant.Success,
  ~label="",
  ~icon: option<module(Lucide.Icon)>=module(Lucide.Empty),
) => {
  module Icon = unpack(icon)

  <div>
    <Icon size={16} />
    {React.string(label)}
  </div>
}

Viewing all articles
Browse latest Browse all 1774

Trending Articles