Quantcast
Viewing all articles
Browse latest Browse all 1813

How to pass a component constructor as a prop

Something like this:

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

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

module IconButton = {
  @react.component
  let make = (~label="", ~renderIcon: iconProps => React.element) =>
    <div>
      {renderIcon({size: 16, color: "white"})}
      {React.string(label)}
    </div>
}

let _ = <IconButton renderIcon=Check.make />
// or, alternatively
let _ = <IconButton renderIcon={props => <Check {...props} />} />

But you lose the nice JSX syntax.


Viewing all articles
Browse latest Browse all 1813

Trending Articles