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

How to pass a component constructor as a prop

$
0
0

Yes! It is possible with first-class modules:

type iconProps = {
  size?: int,
  color?: string,
  strokeWidth?: int,
  absoluteStrokeWidth?: bool,
  className?: string,
}

module type Icon = {
  let make: React.component<iconProps>
}

module Check: Icon = {
  @module("lucide-react")
  external make: React.component<iconProps> = "Check"
}

module IconButton = {
  @react.component
  let make = (~icon: module(Icon)) => {
    module Icon = unpack(icon)

    <button>
      <Icon size={16} color="white" />
    </button>
  }
}

let _ = <IconButton icon={module(Check)} />

Playground link

It’s one of the features that still lacks documentation: Document First Class Modules · Issue #155 · rescript-lang/rescript-lang.org · GitHub

EDIT: Updated it to a full example.


Viewing all articles
Browse latest Browse all 1787

Trending Articles