Return `option<React.element>` in module components

I didn’t know this, but functions with @react.component can’t return an option<React.element>?

This is documented?

If I comment out line 18 there is no type error

module RightSidebar = {
  @react.component
  let make = (~items: array<string>) => {
    switch items->Js.Array2.length > 0 {
    | true =>
      items
      ->Js.Array2.map(item => item->React.string)
      ->React.array
      ->Some
    | false => None
    }
  }
}

module App = {
  @react.component
  let make = () => {
    let a = <RightSidebar items=[] />

    <h1> {"Menu"->React.string} </h1>
  }
}

Playground

React Components must return a React.element, no matter what. Return React.null instead

3 Likes

Makes sense when you think about the js output

1 Like